Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_alloc.h
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef FW_ALLOC_H_
19 #define FW_ALLOC_H_
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif                          /* __cplusplus */
24
25 /**
26  * @file fw_alloc.h
27  * @brief provides allocate operation
28  */
29
30 /** @addtogroup utility
31  *      @{
32  */
33
34 /* data structure alignment */
35 #define UTIL_ALIGN(n, m)                (((n) + ((m) - 1)) & ~((m) - 1))                /**< align*/
36
37 #define UTIL_ALIGN16(n)         UTIL_ALIGN(n, 2)                                                /**< align16*/
38 #define UTIL_ALIGN32(n)         UTIL_ALIGN(n, 4)                                                /**< align32*/
39 #define UTIL_ALIGN64(n)         UTIL_ALIGN(n, 8)                                                /**< align64*/
40
41 /* memory allocation */
42 #ifdef NDEBUG
43 #include <string.h>
44 #include <stdlib.h>
45
46 /* release version */
47         static inline void *UTIL_MALLOC(size_t bytes) {
48                 return malloc(bytes);
49         }
50         static inline void *UTIL_CALLOC(size_t nmemb, size_t size) {
51                 return calloc(nmemb, size);
52         }
53
54         static inline void *UTIL_REALLOC(void *ptr, size_t bytes) {
55                 return realloc(ptr, bytes);
56         }
57
58         static inline char *UTIL_STRDUP(const char *src) {
59                 return strdup(src);
60         }
61
62         static inline char *UTIL_STRNDUP(const char *src, unsigned int n) {
63                 return strndup(src, n);
64         }
65
66 #define UTIL_FREE                free
67
68 #else
69 /* debug version */
70 #define UTIL_MALLOC(bytes)                                                      \
71                 util_malloc(bytes, __FILE__, __LINE__)
72 #define UTIL_CALLOC(nmemb, size)                                                \
73                 util_calloc((nmemb) * (size), __FILE__, __LINE__)
74 #define UTIL_REALLOC(ptr, bytes)                                                \
75                 util_realloc(ptr, bytes, __FILE__, __LINE__)
76 #define UTIL_FREE(ptr)                                                          \
77                 util_free(ptr)
78 #define UTIL_FREE_NO_INFO                                                       \
79                 util_free_no_info
80
81 #define UTIL_STRDUP(src)                                                        \
82                 util_strdup(src, __FILE__, __LINE__)
83 #define UTIL_STRNDUP(src, n)                                                    \
84                 util_strndup(src, n, __FILE__, __LINE__)
85
86 /**
87  * @brief       malloc
88  * @param[in]   bytes           bytes
89  * @param[in]   file            file
90  * @param[in]   line            line
91  * @return              void ptr
92  * @retval              void ptr        success
93  * @retval              NULL            fail
94  */
95         void *util_malloc(unsigned int bytes, const char *file, int line);
96
97 /**
98  * @brief       calloc
99  * @param[in]   bytes           bytes
100  * @param[in]   file            file
101  * @param[in]   line            line
102  * @return              void ptr
103  * @retval              void ptr        success
104  * @retval              NULL            fail
105  */
106         void *util_calloc(unsigned int bytes, const char *file, int line);
107
108 /**
109  * @brief       realloc
110  * @param[in]   ptr             ptr
111  * @param[in]   bytes           bytes
112  * @param[in]   file            file
113  * @param[in]   line            line
114  * @return              void ptr
115  * @retval              void ptr        success
116  * @retval              NULL            fail
117  */
118         void *util_realloc(void *ptr, unsigned int bytes, const char *file, int line);
119
120 /**
121  * @brief       free
122  * @param[in]   ptr                     ptr
123  */
124         void util_free(void *ptr);
125
126 /* TODO */
127
128 /**
129  * @brief       string copy
130  * @param[in]   src             source
131  * @param[in]   file            file
132  * @param[in]   line            line
133  * @return              string
134  * @retval              string  success
135  * @retval              NULL            fail
136  */
137         char *util_strdup(const char *src, const char *file, int line);
138
139 /**
140  * @brief       string copy at most n characters
141  * @param[in]   src             source
142  * @param[in]   n               n characters
143  * @param[in]   file            file
144  * @param[in]   line            line
145  * @return              string
146  * @retval              string  success
147  * @retval              NULL            fail
148  */
149         char *util_strndup(const char *src, unsigned int n, const char *file, int line);
150
151 #endif                          /* no DEBUG_ALLOC */
152
153 /**
154  *      @}
155  */
156
157 #ifdef __cplusplus
158 }
159 #endif                          /* __cplusplus */
160 #endif                          /* FW_ALLOC_H_ */