3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #endif /* __cplusplus */
27 * @brief provides allocate operation
30 /** @addtogroup utility
34 /* data structure alignment */
35 #define UTIL_ALIGN(n, m) (((n) + ((m) - 1)) & ~((m) - 1)) /**< align*/
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*/
41 /* memory allocation */
47 static inline void *UTIL_MALLOC(size_t bytes) {
50 static inline void *UTIL_CALLOC(size_t nmemb, size_t size) {
51 return calloc(nmemb, size);
54 static inline void *UTIL_REALLOC(void *ptr, size_t bytes) {
55 return realloc(ptr, bytes);
58 static inline char *UTIL_STRDUP(const char *src) {
62 static inline char *UTIL_STRNDUP(const char *src, unsigned int n) {
63 return strndup(src, n);
66 #define UTIL_FREE free
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) \
78 #define UTIL_FREE_NO_INFO \
81 #define UTIL_STRDUP(src) \
82 util_strdup(src, __FILE__, __LINE__)
83 #define UTIL_STRNDUP(src, n) \
84 util_strndup(src, n, __FILE__, __LINE__)
88 * @param[in] bytes bytes
89 * @param[in] file file
90 * @param[in] line line
92 * @retval void ptr success
95 void *util_malloc(unsigned int bytes, const char *file, int line);
99 * @param[in] bytes bytes
100 * @param[in] file file
101 * @param[in] line line
103 * @retval void ptr success
106 void *util_calloc(unsigned int bytes, const char *file, int line);
111 * @param[in] bytes bytes
112 * @param[in] file file
113 * @param[in] line line
115 * @retval void ptr success
118 void *util_realloc(void *ptr, unsigned int bytes, const char *file, int line);
124 void util_free(void *ptr);
130 * @param[in] src source
131 * @param[in] file file
132 * @param[in] line line
134 * @retval string success
137 char *util_strdup(const char *src, const char *file, int line);
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
146 * @retval string success
149 char *util_strndup(const char *src, unsigned int n, const char *file, int line);
151 #endif /* no DEBUG_ALLOC */
159 #endif /* __cplusplus */
160 #endif /* FW_ALLOC_H_ */