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,
30                    "assert failed: new_string != nullptr");
31 
32   if (!new_string) return NULL;
33 
34   memcpy(new_string, str, size);
35   return new_string;
36 }
37 
osi_strndup(const char * str,size_t len)38 char* osi_strndup(const char* str, size_t len) {
39   size_t size = strlen(str);
40   if (len < size) size = len;
41 
42   char* new_string = (char*)malloc(size + 1);
43   log::assert_that(new_string != nullptr,
44                    "assert failed: new_string != nullptr");
45 
46   if (!new_string) return NULL;
47 
48   memcpy(new_string, str, size);
49   new_string[size] = '\0';
50   return new_string;
51 }
52 
osi_malloc(size_t size)53 void* osi_malloc(size_t size) {
54   log::assert_that(static_cast<ssize_t>(size) >= 0,
55                    "assert failed: static_cast<ssize_t>(size) >= 0");
56   void* ptr = malloc(size);
57   log::assert_that(ptr != nullptr, "assert failed: ptr != nullptr");
58   return ptr;
59 }
60 
osi_calloc(size_t size)61 void* osi_calloc(size_t size) {
62   log::assert_that(static_cast<ssize_t>(size) >= 0,
63                    "assert failed: static_cast<ssize_t>(size) >= 0");
64   void* ptr = calloc(1, size);
65   log::assert_that(ptr != nullptr, "assert failed: ptr != nullptr");
66   return ptr;
67 }
68 
osi_free(void * ptr)69 void osi_free(void* ptr) { free(ptr); }
70 
osi_free_and_reset(void ** p_ptr)71 void osi_free_and_reset(void** p_ptr) {
72   log::assert_that(p_ptr != NULL, "assert failed: p_ptr != NULL");
73   osi_free(*p_ptr);
74   *p_ptr = NULL;
75 }
76 
77 const allocator_t allocator_calloc = {osi_calloc, osi_free};
78 
79 const allocator_t allocator_malloc = {osi_malloc, osi_free};
80 
OsiObject(void * ptr)81 OsiObject::OsiObject(void* ptr) : ptr_(ptr) {}
82 
OsiObject(const void * ptr)83 OsiObject::OsiObject(const void* ptr) : ptr_(const_cast<void*>(ptr)) {}
84 
~OsiObject()85 OsiObject::~OsiObject() {
86   if (ptr_ != nullptr) {
87     osi_free(ptr_);
88   }
89 }
90 
Release()91 void* OsiObject::Release() {
92   void* ptr = ptr_;
93   ptr_ = nullptr;
94   return ptr;
95 }
96