1*61046927SAndroid Build Coastguard Worker /* 2*61046927SAndroid Build Coastguard Worker * Copyright 2010 Marek Olšák <[email protected]> 3*61046927SAndroid Build Coastguard Worker * Copyright 2016 Advanced Micro Devices, Inc. 4*61046927SAndroid Build Coastguard Worker * 5*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a 6*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"), 7*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation 8*61046927SAndroid Build Coastguard Worker * on the rights to use, copy, modify, merge, publish, distribute, sub 9*61046927SAndroid Build Coastguard Worker * license, and/or sell copies of the Software, and to permit persons to whom 10*61046927SAndroid Build Coastguard Worker * the Software is furnished to do so, subject to the following conditions: 11*61046927SAndroid Build Coastguard Worker * 12*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next 13*61046927SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the 14*61046927SAndroid Build Coastguard Worker * Software. 15*61046927SAndroid Build Coastguard Worker * 16*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19*61046927SAndroid Build Coastguard Worker * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 20*61046927SAndroid Build Coastguard Worker * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 21*61046927SAndroid Build Coastguard Worker * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 22*61046927SAndroid Build Coastguard Worker * USE OR OTHER DEALINGS IN THE SOFTWARE. */ 23*61046927SAndroid Build Coastguard Worker 24*61046927SAndroid Build Coastguard Worker /** 25*61046927SAndroid Build Coastguard Worker * Slab allocator for equally sized memory allocations. 26*61046927SAndroid Build Coastguard Worker * 27*61046927SAndroid Build Coastguard Worker * Objects are allocated from "child" pools that are connected to a "parent" 28*61046927SAndroid Build Coastguard Worker * pool. 29*61046927SAndroid Build Coastguard Worker * 30*61046927SAndroid Build Coastguard Worker * Calls to slab_alloc/slab_free for the same child pool must not occur from 31*61046927SAndroid Build Coastguard Worker * multiple threads simultaneously. 32*61046927SAndroid Build Coastguard Worker * 33*61046927SAndroid Build Coastguard Worker * Allocations obtained from one child pool should usually be freed in the 34*61046927SAndroid Build Coastguard Worker * same child pool. Freeing an allocation in a different child pool associated 35*61046927SAndroid Build Coastguard Worker * to the same parent is allowed (and requires no locking by the caller), but 36*61046927SAndroid Build Coastguard Worker * it is discouraged because it implies a performance penalty. 37*61046927SAndroid Build Coastguard Worker * 38*61046927SAndroid Build Coastguard Worker * For convenience and to ease the transition, there is also a set of wrapper 39*61046927SAndroid Build Coastguard Worker * functions around a single parent-child pair. 40*61046927SAndroid Build Coastguard Worker */ 41*61046927SAndroid Build Coastguard Worker 42*61046927SAndroid Build Coastguard Worker #ifndef SLAB_H 43*61046927SAndroid Build Coastguard Worker #define SLAB_H 44*61046927SAndroid Build Coastguard Worker 45*61046927SAndroid Build Coastguard Worker #include "simple_mtx.h" 46*61046927SAndroid Build Coastguard Worker 47*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus 48*61046927SAndroid Build Coastguard Worker extern "C" { 49*61046927SAndroid Build Coastguard Worker #endif 50*61046927SAndroid Build Coastguard Worker 51*61046927SAndroid Build Coastguard Worker struct slab_element_header; 52*61046927SAndroid Build Coastguard Worker struct slab_page_header; 53*61046927SAndroid Build Coastguard Worker 54*61046927SAndroid Build Coastguard Worker struct slab_parent_pool { 55*61046927SAndroid Build Coastguard Worker simple_mtx_t mutex; 56*61046927SAndroid Build Coastguard Worker unsigned element_size; 57*61046927SAndroid Build Coastguard Worker unsigned num_elements; 58*61046927SAndroid Build Coastguard Worker unsigned item_size; 59*61046927SAndroid Build Coastguard Worker }; 60*61046927SAndroid Build Coastguard Worker 61*61046927SAndroid Build Coastguard Worker struct slab_child_pool { 62*61046927SAndroid Build Coastguard Worker struct slab_parent_pool *parent; 63*61046927SAndroid Build Coastguard Worker 64*61046927SAndroid Build Coastguard Worker struct slab_page_header *pages; 65*61046927SAndroid Build Coastguard Worker 66*61046927SAndroid Build Coastguard Worker /* Free elements. */ 67*61046927SAndroid Build Coastguard Worker struct slab_element_header *free; 68*61046927SAndroid Build Coastguard Worker 69*61046927SAndroid Build Coastguard Worker /* Elements that are owned by this pool but were freed with a different 70*61046927SAndroid Build Coastguard Worker * pool as the argument to slab_free. 71*61046927SAndroid Build Coastguard Worker * 72*61046927SAndroid Build Coastguard Worker * This list is protected by the parent mutex. 73*61046927SAndroid Build Coastguard Worker */ 74*61046927SAndroid Build Coastguard Worker struct slab_element_header *migrated; 75*61046927SAndroid Build Coastguard Worker }; 76*61046927SAndroid Build Coastguard Worker 77*61046927SAndroid Build Coastguard Worker void slab_create_parent(struct slab_parent_pool *parent, 78*61046927SAndroid Build Coastguard Worker unsigned item_size, 79*61046927SAndroid Build Coastguard Worker unsigned num_items); 80*61046927SAndroid Build Coastguard Worker void slab_destroy_parent(struct slab_parent_pool *parent); 81*61046927SAndroid Build Coastguard Worker void slab_create_child(struct slab_child_pool *pool, 82*61046927SAndroid Build Coastguard Worker struct slab_parent_pool *parent); 83*61046927SAndroid Build Coastguard Worker void slab_destroy_child(struct slab_child_pool *pool); 84*61046927SAndroid Build Coastguard Worker void *slab_alloc(struct slab_child_pool *pool); 85*61046927SAndroid Build Coastguard Worker void *slab_zalloc(struct slab_child_pool *pool); 86*61046927SAndroid Build Coastguard Worker void slab_free(struct slab_child_pool *pool, void *ptr); 87*61046927SAndroid Build Coastguard Worker 88*61046927SAndroid Build Coastguard Worker struct slab_mempool { 89*61046927SAndroid Build Coastguard Worker struct slab_parent_pool parent; 90*61046927SAndroid Build Coastguard Worker struct slab_child_pool child; 91*61046927SAndroid Build Coastguard Worker }; 92*61046927SAndroid Build Coastguard Worker 93*61046927SAndroid Build Coastguard Worker void slab_create(struct slab_mempool *mempool, 94*61046927SAndroid Build Coastguard Worker unsigned item_size, 95*61046927SAndroid Build Coastguard Worker unsigned num_items); 96*61046927SAndroid Build Coastguard Worker void slab_destroy(struct slab_mempool *mempool); 97*61046927SAndroid Build Coastguard Worker void *slab_alloc_st(struct slab_mempool *mempool); 98*61046927SAndroid Build Coastguard Worker void slab_free_st(struct slab_mempool *mempool, void *ptr); 99*61046927SAndroid Build Coastguard Worker 100*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus 101*61046927SAndroid Build Coastguard Worker } 102*61046927SAndroid Build Coastguard Worker #endif 103*61046927SAndroid Build Coastguard Worker 104*61046927SAndroid Build Coastguard Worker #endif 105