1/* 2 * Copyright 2016, Data61 3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO) 4 * ABN 41 687 119 230. 5 * 6 * This software may be distributed and modified according to the terms of 7 * the BSD 2-Clause license. Note that NO WARRANTY is provided. 8 * See "LICENSE_BSD2.txt" for details. 9 * 10 * @TAG(D61_BSD) 11 */ 12 13#include <assert.h> 14#include <stdlib.h> 15#include <stdbool.h> 16#include <refos-util/cspace.h> 17#include <refos/vmlayout.h> 18 19/*! @file 20 @brief RefOS client cspace allocator. */ 21 22static seL4_CPtr *cspaceFreeList = NULL; 23static size_t cspaceFreeListNum = 0; 24static bool cspaceStaticAllocated; 25 26void 27csalloc_init(seL4_CPtr start, seL4_CPtr end) 28{ 29 size_t sz = end - start; 30 csalloc_deinit(); 31 cspaceFreeList = malloc(sizeof(seL4_CPtr) * sz); 32 assert(cspaceFreeList); 33 for (int i = 0; i < sz; i++) { 34 cspaceFreeList[i] = (seL4_CPtr)(start + i); 35 } 36 cspaceFreeListNum = sz; 37 cspaceStaticAllocated = false; 38} 39 40void 41csalloc_init_static(seL4_CPtr start, seL4_CPtr end, char* buffer, uint32_t bufferSz) 42{ 43 assert(buffer); 44 45 size_t sz = end - start; 46 assert(sizeof(seL4_CPtr) * sz <= bufferSz); 47 48 csalloc_deinit(); 49 cspaceFreeList = (seL4_CPtr *) buffer; 50 for (int i = 0; i < sz; i++) { 51 cspaceFreeList[i] = (seL4_CPtr)(start + i); 52 } 53 cspaceFreeListNum = sz; 54 cspaceStaticAllocated = true; 55} 56 57void 58csalloc_deinit(void) 59{ 60 if (cspaceFreeList && !cspaceStaticAllocated) { 61 free(cspaceFreeList); 62 } 63 cspaceFreeList = NULL; 64 cspaceFreeListNum = 0; 65} 66 67seL4_CPtr 68csalloc(void) 69{ 70 assert(cspaceFreeList); 71 if (cspaceFreeListNum == 0) { 72 return 0; 73 } 74 return cspaceFreeList[--cspaceFreeListNum]; 75} 76 77void 78csfree(seL4_CPtr c) 79{ 80 assert(cspaceFreeList); 81 cspaceFreeList[cspaceFreeListNum++] = c; 82} 83 84void 85csfree_delete(seL4_CPtr c) 86{ 87 assert(cspaceFreeList); 88 seL4_CNode_Delete(REFOS_CSPACE, c, REFOS_CDEPTH); 89 cspaceFreeList[cspaceFreeListNum++] = c; 90} 91 92 93 94 95