pool.h (3513B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#ifndef _GUAC_POOL_H 21#define _GUAC_POOL_H 22 23/** 24 * Provides functions and structures for maintaining dynamically allocated and 25 * freed pools of integers. 26 * 27 * @file pool.h 28 */ 29 30#include "pool-types.h" 31 32#include <pthread.h> 33 34struct guac_pool { 35 36 /** 37 * The minimum number of integers which must have been returned by 38 * guac_pool_next_int before previously-used and freed integers are 39 * allowed to be returned. 40 */ 41 int min_size; 42 43 /** 44 * The number of integers currently in use. 45 */ 46 int active; 47 48 /** 49 * The next integer to be released (after no more integers remain in the 50 * pool. 51 */ 52 int __next_value; 53 54 /** 55 * The first integer in the pool, if any. 56 */ 57 guac_pool_int* __head; 58 59 /** 60 * The last integer in the pool, if any. 61 */ 62 guac_pool_int* __tail; 63 64 /** 65 * Lock which is acquired when the pool is being modified or accessed. 66 */ 67 pthread_mutex_t __lock; 68 69}; 70 71struct guac_pool_int { 72 73 /** 74 * The integer value of this pool entry. 75 */ 76 int value; 77 78 /** 79 * The next available (unused) guac_pool_int in the list of 80 * allocated but free'd ints. 81 */ 82 guac_pool_int* __next; 83 84}; 85 86/** 87 * Allocates a new guac_pool having the given minimum size. 88 * 89 * @param size The minimum number of integers which must have been returned by 90 * guac_pool_next_int before freed integers (previously used 91 * integers) are allowed to be returned. 92 * @return A new, empty guac_pool, having the given minimum size. 93 */ 94guac_pool* guac_pool_alloc(int size); 95 96/** 97 * Frees the given guac_pool. 98 * 99 * @param pool The guac_pool to free. 100 */ 101void guac_pool_free(guac_pool* pool); 102 103/** 104 * Returns the next available integer from the given guac_pool. All integers 105 * returned are non-negative, and are returned in sequences, starting from 0. 106 * This operation is threadsafe. 107 * 108 * @param pool 109 * The guac_pool to retrieve an integer from. 110 * 111 * @return 112 * The next available integer, which may be either an integer not yet 113 * returned by a call to guac_pool_next_int, or an integer which was 114 * previously returned, but has since been freed. 115 */ 116int guac_pool_next_int(guac_pool* pool); 117 118/** 119 * Frees the given integer back into the given guac_pool. The integer given 120 * will be available for future calls to guac_pool_next_int. This operation is 121 * threadsafe. 122 * 123 * @param pool 124 * The guac_pool to free the given integer into. 125 * 126 * @param value 127 * The integer which should be returned to the given pool, such that it can 128 * be received by a future call to guac_pool_next_int. 129 */ 130void guac_pool_free_int(guac_pool* pool, int value); 131 132#endif 133