1*27b03b36SApple OSS Distributions /* 2*27b03b36SApple OSS Distributions * Copyright (c) 2020-2021 Apple Inc. All rights reserved. 3*27b03b36SApple OSS Distributions * 4*27b03b36SApple OSS Distributions * @APPLE_LICENSE_HEADER_START@ 5*27b03b36SApple OSS Distributions * 6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*27b03b36SApple OSS Distributions * compliance with the License. Please obtain a copy of the License at 10*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this 11*27b03b36SApple OSS Distributions * file. 12*27b03b36SApple OSS Distributions * 13*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are 14*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and 19*27b03b36SApple OSS Distributions * limitations under the License. 20*27b03b36SApple OSS Distributions * 21*27b03b36SApple OSS Distributions * @APPLE_LICENSE_HEADER_END@ 22*27b03b36SApple OSS Distributions */ 23*27b03b36SApple OSS Distributions 24*27b03b36SApple OSS Distributions #ifndef log_mem_h 25*27b03b36SApple OSS Distributions #define log_mem_h 26*27b03b36SApple OSS Distributions 27*27b03b36SApple OSS Distributions #include <stddef.h> 28*27b03b36SApple OSS Distributions #include <stdint.h> 29*27b03b36SApple OSS Distributions #include <sys/param.h> 30*27b03b36SApple OSS Distributions 31*27b03b36SApple OSS Distributions /* 32*27b03b36SApple OSS Distributions * A simple allocator on a top of a plain byte array. Primarily intended to 33*27b03b36SApple OSS Distributions * support OS kernel logging in order to avoid dependency to VM. 34*27b03b36SApple OSS Distributions */ 35*27b03b36SApple OSS Distributions typedef struct logmem_s { 36*27b03b36SApple OSS Distributions lck_spin_t lm_lock; 37*27b03b36SApple OSS Distributions uint8_t *lm_mem; 38*27b03b36SApple OSS Distributions uint8_t *lm_mem_map; 39*27b03b36SApple OSS Distributions size_t lm_mem_size; 40*27b03b36SApple OSS Distributions size_t lm_cap_order; 41*27b03b36SApple OSS Distributions size_t lm_min_order; 42*27b03b36SApple OSS Distributions size_t lm_max_order; 43*27b03b36SApple OSS Distributions uint32_t lm_cnt_allocations; 44*27b03b36SApple OSS Distributions uint32_t lm_cnt_failed_size; 45*27b03b36SApple OSS Distributions uint32_t lm_cnt_failed_full; 46*27b03b36SApple OSS Distributions uint32_t lm_cnt_failed_lmoff; 47*27b03b36SApple OSS Distributions uint32_t lm_cnt_free; 48*27b03b36SApple OSS Distributions } logmem_t; 49*27b03b36SApple OSS Distributions 50*27b03b36SApple OSS Distributions /* 51*27b03b36SApple OSS Distributions * Initializes dynamically allocated logmem. The caller is responsible for 52*27b03b36SApple OSS Distributions * allocating and providing backing memory for both data and the bitmap. 53*27b03b36SApple OSS Distributions * LOGMEM_SIZE and LOGMEM_MAP_SIZE provide a way to determine the right sizes. 54*27b03b36SApple OSS Distributions */ 55*27b03b36SApple OSS Distributions void logmem_init(logmem_t *, void *, size_t, size_t, size_t, size_t); 56*27b03b36SApple OSS Distributions 57*27b03b36SApple OSS Distributions /* 58*27b03b36SApple OSS Distributions * Returns true if the logmem is initialized, false otherwise. 59*27b03b36SApple OSS Distributions */ 60*27b03b36SApple OSS Distributions bool logmem_ready(const logmem_t *); 61*27b03b36SApple OSS Distributions 62*27b03b36SApple OSS Distributions /* 63*27b03b36SApple OSS Distributions * Allocates memory from a respective logmem. Returns a pointer to the beginning 64*27b03b36SApple OSS Distributions * of the allocated block. The resulting size of the allocated block is equal or 65*27b03b36SApple OSS Distributions * bigger than the size passed in during the call. The function comes in two 66*27b03b36SApple OSS Distributions * flavours, locking and non-locking. The caller is responsible for choosing the 67*27b03b36SApple OSS Distributions * right one based on where and how the logmem is used. 68*27b03b36SApple OSS Distributions */ 69*27b03b36SApple OSS Distributions void *logmem_alloc(logmem_t *, size_t *); 70*27b03b36SApple OSS Distributions void *logmem_alloc_locked(logmem_t *, size_t *); 71*27b03b36SApple OSS Distributions 72*27b03b36SApple OSS Distributions /* 73*27b03b36SApple OSS Distributions * Frees memory previously allocated by logmem_alloc*(). The caller must call 74*27b03b36SApple OSS Distributions * logmem_free*() with exact pointer and size value returned by logmem_alloc*(). 75*27b03b36SApple OSS Distributions * The function comes in two flavours, locking and non-locking. The caller is 76*27b03b36SApple OSS Distributions * responsible for choosing the right one based on where and how the logmem is 77*27b03b36SApple OSS Distributions * used. 78*27b03b36SApple OSS Distributions */ 79*27b03b36SApple OSS Distributions void logmem_free(logmem_t *, void *, size_t); 80*27b03b36SApple OSS Distributions void logmem_free_locked(logmem_t *, void *, size_t); 81*27b03b36SApple OSS Distributions 82*27b03b36SApple OSS Distributions /* 83*27b03b36SApple OSS Distributions * Returns the maximum memory size allocatable by the logmem. 84*27b03b36SApple OSS Distributions */ 85*27b03b36SApple OSS Distributions size_t logmem_max_size(const logmem_t *); 86*27b03b36SApple OSS Distributions 87*27b03b36SApple OSS Distributions /* 88*27b03b36SApple OSS Distributions * Returns true if logmem is empty, false otherwise. 89*27b03b36SApple OSS Distributions */ 90*27b03b36SApple OSS Distributions bool logmem_empty(const logmem_t *); 91*27b03b36SApple OSS Distributions 92*27b03b36SApple OSS Distributions /* 93*27b03b36SApple OSS Distributions * Returns an amount of memory the logmem needs to be initialized with in order 94*27b03b36SApple OSS Distributions * to provide allocations and to maintain its internal state. The caller should 95*27b03b36SApple OSS Distributions * use this function to get the right amount, allocate the memory accodingly and 96*27b03b36SApple OSS Distributions * pass it to logmem_init(). 97*27b03b36SApple OSS Distributions */ 98*27b03b36SApple OSS Distributions size_t logmem_required_size(size_t, size_t); 99*27b03b36SApple OSS Distributions 100*27b03b36SApple OSS Distributions #endif /* log_mem_h */ 101