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