All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
src/include/aerospike/as_memtracker.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 <stdarg.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 /*****************************************************************************
30  * TYPES
31  *****************************************************************************/
32 
34 typedef struct as_memtracker_hooks_s as_memtracker_hooks;
35 
36 struct as_memtracker_s;
37 typedef struct as_memtracker_s as_memtracker;
38 
39 /**
40  * The interface which all memtrackers should implement.
41  */
43  /**
44  * The destroy should free resources associated with the memtracker's source.
45  * The destroy should not free the memtracker itself.
46  */
47  int (* destroy)(as_memtracker *);
48 
49  bool (* reserve)(const as_memtracker *, const uint32_t);
50  bool (* release)(const as_memtracker *, const uint32_t);
51  bool (* reset)(const as_memtracker *);
52 };
53 
54 /**
55  * Logger handle
56  */
58  bool is_malloc;
59  void * source;
60  const as_memtracker_hooks * hooks;
61 };
62 
63 /*****************************************************************************
64  * FUNCTIONS
65  *****************************************************************************/
66 
67 /**
68  * Initialize a stack allocated memtracker
69  */
70 as_memtracker * as_memtracker_init(as_memtracker * memtracker, void * source, const as_memtracker_hooks * hooks);
71 
72 /**
73  * Heap allocate and initialize a memtracker
74  */
75 as_memtracker * as_memtracker_new(void * source, const as_memtracker_hooks * hooks);
76 
77 
78 inline void * as_memtracker_source(const as_memtracker * mt) {
79  return (mt ? mt->source : NULL);
80 }
81 
82 /**
83  * Release resources associated with the memtracker.
84  * Calls memtracker->destroy. If success and if this is a heap allocated
85  * memtracker, then it will be freed.
86  */
87 int as_memtracker_destroy(as_memtracker * memtracker);
88 
89 /**
90  * Reserve num_bytes bytes of memory
91  */
92 bool as_memtracker_reserve(const as_memtracker * memtracker, const uint32_t num_bytes);
93 
94 /**
95  * Release num_bytes bytes of memory
96  */
97 bool as_memtracker_release(const as_memtracker * memtracker, const uint32_t num_bytes);
98 
99 /**
100  * Release the entire reservation for the current thread
101  */
102 bool as_memtracker_reset(const as_memtracker * memtracker);
103