HomePort
hpd_common.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #ifndef HOMEPORT_HPD_COMMON_H
29 #define HOMEPORT_HPD_COMMON_H
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
44 #define HPD_CALLOC(PTR, NUM, CAST) do { \
45  (PTR) = (CAST *) calloc((NUM), sizeof(CAST)); \
46  if(!(PTR)) goto alloc_error; \
47 } while(0)
48 
52 #define HPD_REALLOC(PTR, NUM, CAST) do { \
53  void *_tmp = realloc((PTR), (NUM)*sizeof(CAST)); \
54  if(!_tmp) goto alloc_error; \
55  (PTR) = (CAST *) _tmp; \
56 } while(0)
57 
58 #define HPD_CPY_ALLOC(DST, SRC, CAST) do { \
59  (DST) = (CAST *) malloc(sizeof(CAST)); \
60  if(!(DST)) goto alloc_error; \
61  memcpy((DST), (SRC), sizeof(CAST)); \
62 } while(0)
63 
64 #define HPD_STR_CPY(DST, SRC) do { \
65  HPD_REALLOC((DST), (strlen((SRC))+1), char); \
66  strcpy((DST), (SRC)); \
67 } while(0)
68 
69 #define HPD_STR_N_CPY(DST, SRC, LEN) do { \
70  HPD_REALLOC((DST), ((LEN)+1), char); \
71  strncpy((DST), (SRC), (LEN)); \
72  (DST)[LEN] = '\0'; \
73 } while(0)
74 
75 #define HPD_SPRINTF_ALLOC(DST, FMT, ...) do { \
76  size_t _len; \
77  if ((_len = snprintf(NULL, 0, (FMT), ##__VA_ARGS__)) < 0) goto snprintf_error; \
78  if (!((DST) = calloc(_len+1, sizeof(char)))) goto alloc_error; \
79  if (snprintf((DST), _len+1, (FMT), ##__VA_ARGS__) < 0) { free((DST)); goto snprintf_error; } \
80 } while (0)
81 
82 #define HPD_VSPRINTF_ALLOC(DST, FMT, VP) do { \
83  size_t _len; \
84  va_list _vp; \
85  va_copy(_vp, (VP)); \
86  if ((_len = vsnprintf(NULL, 0, (FMT), _vp)) < 0) { \
87  va_end(_vp); \
88  goto vsnprintf_error; \
89  } \
90  va_end(_vp); \
91  if (!((DST) = calloc(_len+1, sizeof(char)))) goto alloc_error; \
92  if (vsnprintf((DST), _len+1, (FMT), (VP)) < 0) { free((DST)); goto vsnprintf_error; } \
93 } while (0)
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #endif //HOMEPORT_HPD_COMMON_H