| 1 | /* |
| 2 | * Copyright 2008-2018 Aerospike, Inc. |
| 3 | * |
| 4 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 5 | * license agreements. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 | * use this file except in compliance with the License. You may obtain a copy of |
| 9 | * the License at 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, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations under |
| 15 | * the License. |
| 16 | */ |
| 17 | |
| 18 | /* |
| 19 | * NB: Compile this default memory allocator only if the enhanced memory allocator is *NOT* enabled. |
| 20 | */ |
| 21 | #ifndef ENHANCED_ALLOC |
| 22 | |
| 23 | #include <citrusleaf/alloc.h> |
| 24 | #include <aerospike/as_atomic.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | void* |
| 29 | cf_malloc(size_t sz) |
| 30 | { |
| 31 | return malloc(sz); |
| 32 | } |
| 33 | |
| 34 | void* |
| 35 | cf_calloc(size_t nmemb, size_t sz) |
| 36 | { |
| 37 | return calloc(nmemb, sz); |
| 38 | } |
| 39 | |
| 40 | void* |
| 41 | cf_realloc(void *ptr, size_t sz) |
| 42 | { |
| 43 | return realloc(ptr,sz); |
| 44 | } |
| 45 | |
| 46 | void* |
| 47 | cf_strdup(const char *s) |
| 48 | { |
| 49 | return strdup(s); |
| 50 | } |
| 51 | |
| 52 | void* |
| 53 | cf_strndup(const char *s, size_t n) |
| 54 | { |
| 55 | #if defined(_MSC_VER) |
| 56 | size_t len = strnlen(s, n); |
| 57 | char* t = cf_malloc(len + 1); |
| 58 | |
| 59 | if (t == NULL) { |
| 60 | return NULL; |
| 61 | } |
| 62 | t[len] = 0; |
| 63 | return memcpy(t, s, len); |
| 64 | #else |
| 65 | return strndup(s, n); |
| 66 | #endif |
| 67 | } |
| 68 | |
| 69 | void* |
| 70 | cf_valloc(size_t sz) |
| 71 | { |
| 72 | #if defined(_MSC_VER) || defined(__FreeBSD__) |
| 73 | // valloc is not used by the client. |
| 74 | // Since this file is for the client only, just return null. |
| 75 | return NULL; |
| 76 | #else |
| 77 | return valloc(sz); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | cf_free(void *p) |
| 83 | { |
| 84 | free(p); |
| 85 | } |
| 86 | |
| 87 | int |
| 88 | cf_rc_reserve(void* addr) |
| 89 | { |
| 90 | cf_rc_hdr* head = (cf_rc_hdr*)addr - 1; |
| 91 | return as_aaf_uint32(&head->count, 1); |
| 92 | } |
| 93 | |
| 94 | void* |
| 95 | cf_rc_alloc(size_t sz) |
| 96 | { |
| 97 | cf_rc_hdr* head = malloc(sizeof(cf_rc_hdr) + sz); |
| 98 | |
| 99 | as_store_uint32(&head->count, 1); // Need atomic store? |
| 100 | head->sz = (uint32_t)sz; |
| 101 | |
| 102 | return head + 1; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | cf_rc_free(void* addr) |
| 107 | { |
| 108 | cf_rc_hdr* head = (cf_rc_hdr*)addr - 1; |
| 109 | free(head); |
| 110 | } |
| 111 | |
| 112 | int |
| 113 | cf_rc_release(void* addr) |
| 114 | { |
| 115 | cf_rc_hdr* head = (cf_rc_hdr*)addr - 1; |
| 116 | return (int)as_aaf_uint32(&head->count, -1); |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | cf_rc_releaseandfree(void* addr) |
| 121 | { |
| 122 | cf_rc_hdr* head = (cf_rc_hdr*)addr - 1; |
| 123 | int rc = (int)as_aaf_uint32(&head->count, -1); |
| 124 | |
| 125 | if (rc == 0) { |
| 126 | free(head); |
| 127 | } |
| 128 | |
| 129 | return rc; |
| 130 | } |
| 131 | |
| 132 | #endif // defined(ENHANCED_ALLOC) |
| 133 | |