Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / c_common / oic_malloc / src / oic_malloc.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 //-----------------------------------------------------------------------------
22 // Includes
23 //-----------------------------------------------------------------------------
24 #include <stdlib.h>
25 #include "oic_malloc.h"
26
27 // Enable extra debug logging for malloc.  Comment out to disable
28 #ifdef ENABLE_MALLOC_DEBUG
29 #include "logger.h"
30 #define TAG "OICMalloc"
31 #endif
32
33 //-----------------------------------------------------------------------------
34 // Typedefs
35 //-----------------------------------------------------------------------------
36
37 //-----------------------------------------------------------------------------
38 // Private variables
39 //-----------------------------------------------------------------------------
40
41 //-----------------------------------------------------------------------------
42 // Macros
43 //-----------------------------------------------------------------------------
44
45 //-----------------------------------------------------------------------------
46 // Internal API function
47 //-----------------------------------------------------------------------------
48
49 //-----------------------------------------------------------------------------
50 // Private internal function prototypes
51 //-----------------------------------------------------------------------------
52
53 //-----------------------------------------------------------------------------
54 // Public APIs
55 //-----------------------------------------------------------------------------
56 #ifdef ENABLE_MALLOC_DEBUG
57 static uint32_t count;
58 #endif
59
60 void *OICMalloc(size_t size)
61 {
62 #ifdef ENABLE_MALLOC_DEBUG
63     void *ptr = 0;
64
65     if (0 == size)
66     {
67         return NULL;
68     }
69
70     ptr = malloc(size);
71     count++;
72     OIC_LOG_V(INFO, TAG, "malloc: ptr=%p, size=%u, count=%u", ptr, size, count);
73     return ptr;
74 #else
75     if (0 == size)
76     {
77         return NULL;
78     }
79     return malloc(size);
80 #endif
81 }
82
83 void *OICCalloc(size_t num, size_t size)
84 {
85     if(0 == size || 0 == num)
86     {
87         return NULL;
88     }
89
90 #ifdef ENABLE_MALLOC_DEBUG
91     void *ptr = 0;
92
93     ptr = calloc(num, size);
94     OIC_LOG_V(INFO, TAG, "calloc: ptr=%p, num=%u, size=%u", ptr, num, size);
95     return ptr;
96 #else
97     return calloc(num, size);
98 #endif
99 }
100
101 void *OICRealloc(void* ptr, size_t size)
102 {
103     if(size == 0)
104     {
105         OICFree(ptr);
106         return NULL;
107     }
108
109 #ifdef ENABLE_MALLOC_DEBUG
110     if(ptr == NULL)
111     {
112         return OICMalloc(size);
113     }
114
115     void* newptr = NULL;
116     newptr = realloc(ptr, size);
117     OIC_LOG_V(INFO, TAG, "realloc: ptr=%p, newptr=%p, size=%u", ptr, newptr, size);
118     return ptr;
119 #else
120     return realloc(ptr, size);
121 #endif
122 }
123
124 void OICFree(void *ptr)
125 {
126 #ifdef ENABLE_MALLOC_DEBUG
127     OIC_LOG_V(INFO, TAG, "free: ptr=%p, count=%u", ptr, --count);
128 #endif
129
130     free(ptr);
131 }
132
133