1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _SLAB_H_ 18 #define _SLAB_H_ 19 20 #include <stdint.h> 21 22 struct SlabAllocator; 23 24 25 26 //thread/interrupt safe. allocations will not fail if space exists. even in interrupts. 27 //itemAlign over 4 will not be guaranteed since the heap does not hand out chunks with that kind of alignment 28 struct SlabAllocator* slabAllocatorNew(uint32_t itemSz, uint32_t itemAlign, uint32_t numItems); 29 void slabAllocatorDestroy(struct SlabAllocator *allocator); 30 void* slabAllocatorAlloc(struct SlabAllocator *allocator); 31 void slabAllocatorFree(struct SlabAllocator *allocator, void *ptr); 32 33 void* slabAllocatorGetNth(struct SlabAllocator *allocator, uint32_t idx); // -> pointer or NULL if that slot is empty may be not int-safe. YMMV 34 uint32_t slabAllocatorGetIndex(struct SlabAllocator *allocator, void *ptr); // -> index or -1 if invalid pointer 35 uint32_t slabAllocatorGetNumItems(struct SlabAllocator *allocator); // simply say hwo many items it can hold max (numItems passed to constructor) 36 37 #endif 38 39