Merge branch 'resource-container'
[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 "OIC_MALLOC"
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     if (0 == size)
63     {
64         return NULL;
65     }
66
67 #ifdef ENABLE_MALLOC_DEBUG
68     void *ptr = malloc(size);
69     if (ptr)
70     {
71         count++;
72     }
73     OIC_LOG_V(INFO, TAG, "malloc: ptr=%p, size=%u, count=%u", ptr, size, count);
74     return ptr;
75 #else
76     return malloc(size);
77 #endif
78 }
79
80 void *OICCalloc(size_t num, size_t size)
81 {
82     if (0 == size || 0 == num)
83     {
84         return NULL;
85     }
86
87 #ifdef ENABLE_MALLOC_DEBUG
88     void *ptr = calloc(num, size);
89     if (ptr)
90     {
91         count++;
92     }
93     OIC_LOG_V(INFO, TAG, "calloc: ptr=%p, num=%u, size=%u, count=%u", ptr, num, size, count);
94     return ptr;
95 #else
96     return calloc(num, size);
97 #endif
98 }
99
100 void *OICRealloc(void* ptr, size_t size)
101 {
102     // Override realloc() behavior for NULL pointer which normally would
103     // work as per malloc(), however we suppress the behavior of possibly
104     // returning a non-null unique pointer.
105     if (ptr == NULL)
106     {
107         return OICMalloc(size);
108     }
109
110     // Otherwise leave the behavior up to realloc() itself:
111
112 #ifdef ENABLE_MALLOC_DEBUG
113     void* newptr = realloc(ptr, size);
114     OIC_LOG_V(INFO, TAG, "realloc: ptr=%p, newptr=%p, size=%u", ptr, newptr, size);
115     // Very important to return the correct pointer here, as it only *somtimes*
116     // differs and thus can be hard to notice/test:
117     return newptr;
118 #else
119     return realloc(ptr, size);
120 #endif
121 }
122
123 void OICFree(void *ptr)
124 {
125 #ifdef ENABLE_MALLOC_DEBUG
126     // Since OICMalloc() did not increment count if it returned NULL,
127     // guard the decrement:
128     if (ptr)
129     {
130         count--;
131     }
132     OIC_LOG_V(INFO, TAG, "free: ptr=%p, count=%u", ptr, count);
133 #endif
134
135     free(ptr);
136 }