OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / osi / src / allocator.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
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 #include <stdlib.h>
19 #include <string.h>
20
21 #include "osi/include/allocator.h"
22 #include "osi/include/allocation_tracker.h"
23
24 static const allocator_id_t alloc_allocator_id = 42;
25
26 char *osi_strdup(const char *str) {
27   size_t size = strlen(str) + 1;  // + 1 for the null terminator
28   size_t real_size = allocation_tracker_resize_for_canary(size);
29
30   char *new_string = allocation_tracker_notify_alloc(
31       alloc_allocator_id,
32       malloc(real_size),
33       size);
34   if (!new_string)
35     return NULL;
36
37   memcpy(new_string, str, size);
38   return new_string;
39 }
40
41 char *osi_strndup(const char *str, size_t len) {
42   size_t size = strlen(str);
43   if (len < size)
44     size = len;
45
46   size_t real_size = allocation_tracker_resize_for_canary(size + 1);
47
48   char *new_string = allocation_tracker_notify_alloc(
49       alloc_allocator_id,
50       malloc(real_size),
51       size + 1);
52   if (!new_string)
53     return NULL;
54
55   memcpy(new_string, str, size);
56   new_string[size] = '\0';
57   return new_string;
58 }
59
60 void *osi_malloc(size_t size) {
61   size_t real_size = allocation_tracker_resize_for_canary(size);
62   return allocation_tracker_notify_alloc(
63     alloc_allocator_id,
64     malloc(real_size),
65     size);
66 }
67
68 void *osi_calloc(size_t size) {
69   size_t real_size = allocation_tracker_resize_for_canary(size);
70   return allocation_tracker_notify_alloc(
71     alloc_allocator_id,
72     calloc(1, real_size),
73     size);
74 }
75
76 void osi_free(void *ptr) {
77   free(allocation_tracker_notify_free(alloc_allocator_id, ptr));
78 }
79
80 const allocator_t allocator_malloc = {
81   osi_malloc,
82   osi_free
83 };
84
85 const allocator_t allocator_calloc = {
86   osi_calloc,
87   osi_free
88 };