1 /******************************************************************************
2  *
3  *  Copyright 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 "osi/include/allocator.h"
19 
20 #include <bluetooth/log.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 using namespace bluetooth;
25 
osi_strdup(const char * str)26 char* osi_strdup(const char* str) {
27   size_t size = strlen(str) + 1;  // + 1 for the null terminator
28   char* new_string = (char*)malloc(size);
29   log::assert_that(new_string != nullptr, "assert failed: new_string != nullptr");
30 
31   if (!new_string) {
32     return NULL;
33   }
34 
35   memcpy(new_string, str, size);
36   return new_string;
37 }
38 
osi_strndup(const char * str,size_t len)39 char* osi_strndup(const char* str, size_t len) {
40   size_t size = strlen(str);
41   if (len < size) {
42     size = len;
43   }
44 
45   char* new_string = (char*)malloc(size + 1);
46   log::assert_that(new_string != nullptr, "assert failed: new_string != nullptr");
47 
48   if (!new_string) {
49     return NULL;
50   }
51 
52   memcpy(new_string, str, size);
53   new_string[size] = '\0';
54   return new_string;
55 }
56 
osi_malloc(size_t size)57 void* osi_malloc(size_t size) {
58   log::assert_that(static_cast<ssize_t>(size) >= 0,
59                    "assert failed: static_cast<ssize_t>(size) >= 0");
60   void* ptr = malloc(size);
61   log::assert_that(ptr != nullptr, "assert failed: ptr != nullptr");
62   return ptr;
63 }
64 
osi_calloc(size_t size)65 void* osi_calloc(size_t size) {
66   log::assert_that(static_cast<ssize_t>(size) >= 0,
67                    "assert failed: static_cast<ssize_t>(size) >= 0");
68   void* ptr = calloc(1, size);
69   log::assert_that(ptr != nullptr, "assert failed: ptr != nullptr");
70   return ptr;
71 }
72 
osi_free(void * ptr)73 void osi_free(void* ptr) { free(ptr); }
74 
osi_free_and_reset(void ** p_ptr)75 void osi_free_and_reset(void** p_ptr) {
76   log::assert_that(p_ptr != NULL, "assert failed: p_ptr != NULL");
77   osi_free(*p_ptr);
78   *p_ptr = NULL;
79 }
80 
81 const allocator_t allocator_calloc = {osi_calloc, osi_free};
82 
83 const allocator_t allocator_malloc = {osi_malloc, osi_free};
84 
OsiObject(void * ptr)85 OsiObject::OsiObject(void* ptr) : ptr_(ptr) {}
86 
OsiObject(const void * ptr)87 OsiObject::OsiObject(const void* ptr) : ptr_(const_cast<void*>(ptr)) {}
88 
~OsiObject()89 OsiObject::~OsiObject() {
90   if (ptr_ != nullptr) {
91     osi_free(ptr_);
92   }
93 }
94 
Release()95 void* OsiObject::Release() {
96   void* ptr = ptr_;
97   ptr_ = nullptr;
98   return ptr;
99 }
100