All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_buffer_pool.h
Go to the documentation of this file.
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 #include <citrusleaf/cf_queue.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26  /******************************************************************************
27  * TYPES
28  *****************************************************************************/
29 
30  /**
31  * @private
32  * Buffer.
33  */
34  typedef struct as_buffer_result_s {
35  void* data;
36  uint32_t capacity;
38 
39  /**
40  * @private
41  * Buffer pool.
42  */
43  typedef struct as_buffer_pool_s {
44  cf_queue* queue;
45  uint32_t header_size;
46  uint32_t buffer_size;
48 
49  /******************************************************************************
50  * FUNCTIONS
51  *****************************************************************************/
52 
53  /**
54  * @private
55  * Initialize empty buffer pool. Each buffer in the pool will be a fixed size.
56  *
57  * @param pool Buffer pool.
58  * @param header_size Size of buffer header.
59  * @param buffer_size Fixed buffer size.
60  *
61  * Returns:
62  * 0 : Success
63  * -1 : Failed to create queue.
64  */
65  int
66  as_buffer_pool_init(as_buffer_pool* pool, uint32_t header_size, uint32_t buffer_size);
67 
68  /**
69  * @private
70  * If requested buffer size is less than/equal the pool's buffer size, pop buffer from pool.
71  * Otherwise allocate memory on heap. If the pool is empty, also create buffer on heap.
72  *
73  * @param pool Buffer pool.
74  * @param size Requested size of buffer.
75  * @param buffer Buffer to be populated.
76  *
77  * Returns:
78  * 0 : Found in pool.
79  * 1 : Pool empty. Allocated new buffer.
80  * 2 : Size greater than capacity. Allocated new large buffer.
81  * -1 : Memory allocation error.
82  * -2 : Queue failure.
83  */
84  int
85  as_buffer_pool_pop(as_buffer_pool* pool, uint32_t size, as_buffer_result* buffer);
86 
87  /**
88  * @private
89  * If buffer capacity less than/equal the pool's buffer size, push buffer back into pool.
90  * Otherwise, free memory and do not put back into pool.
91  *
92  * @param pool Buffer pool.
93  * @param buffer Buffer.
94  * @param capacity Capacity of buffer.
95  *
96  * Returns:
97  * 0 : Success
98  * -1 : Queue failure.
99  * -2 : Detected large buffer of different size. Buffer was destroyed.
100  */
101  int
102  as_buffer_pool_push(as_buffer_pool* pool, void* buffer, uint32_t capacity);
103 
104  /**
105  * @private
106  * If buffer capacity less than/equal the pool's buffer size and the number of unused buffers
107  * is less than/equal than max_buffers, push buffer back into pool.
108  * Otherwise, free memory and do not put back into pool.
109  *
110  * @param pool Buffer pool.
111  * @param buffer Buffer.
112  * @param capacity Capacity of buffer.
113  * @param max_buffers Maximum number of unused buffers allowed in pool.
114  *
115  * Returns:
116  * 0 : Success
117  * -1 : Too many buffers exist in pool. Buffer was destroyed.
118  * -2 : Detected large buffer of different size. Buffer was destroyed.
119  */
120  int
121  as_buffer_pool_push_limit(as_buffer_pool* pool, void* buffer, uint32_t capacity, uint32_t max_buffers);
122 
123  /**
124  * @private
125  * Delete buffer_count buffers from the buffer pool. This is useful when a large number of
126  * buffers are created due to a burst of concurrent buffer usage and pruning is desired.
127  *
128  * @param pool Buffer pool.
129  * @param buffer_count Number of buffers to delete.
130  *
131  * Returns number of buffers deleted.
132  */
133  int
134  as_buffer_pool_drop_buffers(as_buffer_pool* pool, int buffer_count);
135 
136  /**
137  * @private
138  * Empty buffer pool and destroy.
139  *
140  * @param pool Buffer pool.
141  */
142  void
144 
145 #ifdef __cplusplus
146 } // end extern "C"
147 #endif
int as_buffer_pool_init(as_buffer_pool *pool, uint32_t header_size, uint32_t buffer_size)
int as_buffer_pool_drop_buffers(as_buffer_pool *pool, int buffer_count)
int as_buffer_pool_push_limit(as_buffer_pool *pool, void *buffer, uint32_t capacity, uint32_t max_buffers)
cf_queue * queue
int as_buffer_pool_push(as_buffer_pool *pool, void *buffer, uint32_t capacity)
uint32_t header_size
int as_buffer_pool_pop(as_buffer_pool *pool, uint32_t size, as_buffer_result *buffer)
uint32_t buffer_size
void as_buffer_pool_destroy(as_buffer_pool *pool)