| 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 | #pragma once |
| 18 | |
| 19 | #include <aerospike/as_std.h> |
| 20 | |
| 21 | #ifdef __cplusplus |
| 22 | extern "C" { |
| 23 | #endif |
| 24 | |
| 25 | #ifdef MEM_COUNT |
| 26 | |
| 27 | #include "mem_count.h" |
| 28 | |
| 29 | #endif // defined(MEM_COUNT) |
| 30 | |
| 31 | /* |
| 32 | * Trivial hash function for storing 64-bit values in hash tables. |
| 33 | */ |
| 34 | static inline uint32_t |
| 35 | ptr_hash_fn(const void *key) |
| 36 | { |
| 37 | return (uint32_t) * (const uint64_t *) key; |
| 38 | } |
| 39 | |
| 40 | #ifdef ENHANCED_ALLOC |
| 41 | |
| 42 | #include "enhanced_alloc.h" |
| 43 | |
| 44 | #else // !defined(ENHANCED_ALLOC) |
| 45 | |
| 46 | /* |
| 47 | * CF Memory Allocation-Related Functions: |
| 48 | * |
| 49 | * These functions simply wrap the C standard library memory allocation-related functions. |
| 50 | */ |
| 51 | |
| 52 | AS_EXTERN void* cf_malloc(size_t sz); |
| 53 | AS_EXTERN void* cf_calloc(size_t nmemb, size_t sz); |
| 54 | AS_EXTERN void* cf_realloc(void* ptr, size_t sz); |
| 55 | AS_EXTERN void* cf_strdup(const char* s); |
| 56 | AS_EXTERN void* cf_strndup(const char* s, size_t n); |
| 57 | AS_EXTERN void* cf_valloc(size_t sz); |
| 58 | AS_EXTERN void cf_free(void* p); |
| 59 | |
| 60 | /* |
| 61 | * The "cf_rc_*()" Functions: Reference Counting Allocation: |
| 62 | * |
| 63 | * This extends the traditional C memory allocation system to support |
| 64 | * reference-counted garbage collection. When a memory region is allocated |
| 65 | * via cf_rc_alloc(), slightly more memory than was requested is actually |
| 66 | * allocated. A reference counter is inserted in the excess space at the |
| 67 | * at the front of the region, and a pointer to the first byte of the data |
| 68 | * allocation is returned. |
| 69 | * |
| 70 | * Two additional functions are supplied to support using a reference |
| 71 | * counted region: cf_rc_reserve() reserves a memory region, and |
| 72 | * cf_rc_release() releases an already-held reservation. It is possible to |
| 73 | * call cf_rc_release() on a region without first acquiring a reservation. |
| 74 | * This will result in undefined behavior. |
| 75 | */ |
| 76 | |
| 77 | typedef struct { |
| 78 | uint32_t count; |
| 79 | uint32_t sz; |
| 80 | } cf_rc_hdr; |
| 81 | |
| 82 | void* cf_rc_alloc(size_t sz); |
| 83 | void cf_rc_free(void* addr); |
| 84 | int cf_rc_reserve(void* addr); |
| 85 | int cf_rc_release(void* addr); |
| 86 | int cf_rc_releaseandfree(void* addr); |
| 87 | |
| 88 | #endif // defined(ENHANCED_ALLOC) |
| 89 | |
| 90 | #ifdef __cplusplus |
| 91 | } // end extern "C" |
| 92 | #endif |
| 93 | |