All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
src/include/citrusleaf/cf_client_rc.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 <stddef.h>
25 
26 #include <citrusleaf/cf_atomic.h>
27 #include <citrusleaf/cf_types.h>
28 
29 
30 /* SYNOPSIS
31  * Reference counting allocation
32  * This extends the traditional C memory allocation system to support
33  * reference-counted garbage collection. When a memory region is allocated
34  * via cf_rc_alloc(), slightly more memory than was requested is actually
35  * allocated. A reference counter is inserted in the excess space at the
36  * at the front of the region, and a pointer to the first byte of the data
37  * allocation is returned.
38  *
39  * Two additional functions are supplied to support using a reference
40  * counted region: cf_client_rc_reserve() reserves a memory region, and
41  * cf_client_rc_release() releases an already-held reservation. It is possible to
42  * call cf_client_rc_release() on a region without first acquiring a reservation.
43  * This will result in undefined behavior.
44  */
45 
46 
47 
48 /* cf_client_rc_counter
49  * A reference counter */
50 typedef cf_atomic32 cf_client_rc_counter;
51 
52 /* Function declarations */
53 extern cf_atomic_int_t cf_client_rc_count(void *addr);
54 extern void *cf_client_rc_alloc(size_t sz);
55 extern int cf_client_rc_reserve(void *addr);
56 extern cf_atomic_int_t cf_client_rc_release_x(void *addr, bool autofree);
57 extern void cf_client_rc_free(void *addr);
58 #define cf_client_rc_release(a) (cf_client_rc_release_x((a), false))
59 #define cf_client_rc_releaseandfree(a) (cf_client_rc_release_x((a), true))
60