All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modules/common/target/Linux-x86_64/include/citrusleaf/alloc.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
24 #include <stdlib.h>
25 #include <citrusleaf/cf_atomic.h>
26 
27 #ifdef MEM_COUNT
28 
29 #include <citrusleaf/mem_count.h>
30 
31 #endif // defined(MEM_COUNT)
32 
33 /*
34  * Trivial hash function for storing 64-bit values in hash tables.
35  */
36 static inline uint32_t ptr_hash_fn(void *key) {
37  return (uint32_t) * (uint64_t *) key;
38 }
39 
40 #ifdef ENHANCED_ALLOC
41 
42 #include <citrusleaf/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 void *cf_malloc(size_t sz);
53 void *cf_calloc(size_t nmemb, size_t sz);
54 void *cf_realloc(void *ptr, size_t sz);
55 void *cf_strdup(const char *s);
56 void *cf_strndup(const char *s, size_t n);
57 void *cf_valloc(size_t sz);
58 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 cf_atomic32 cf_rc_counter;
78 
79 typedef struct {
80  cf_rc_counter count;
81  uint32_t sz;
82 } cf_rc_hdr;
83 
84 void *cf_rc_alloc(size_t sz);
85 void cf_rc_free(void *addr);
86 cf_atomic_int_t cf_rc_count(void *addr);
87 int cf_rc_reserve(void *addr);
88 int cf_rc_release(void *addr);
89 int cf_rc_releaseandfree(void *addr);
90 
91 #endif // defined(ENHANCED_ALLOC)