]>
git.saurik.com Git - apple/xnu.git/blob - libkern/os/log_mem.h
d29ca2b19926a4af146d7f4aa46d02cc77b6fd85
2 * Copyright (c) 2020 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
31 * A simple allocator on a top of a plain byte array. Primarily intended to
32 * support OS kernel logging in order to avoid dependency to VM.
34 typedef struct logmem_s
{
41 uint32_t lm_cnt_allocations
;
42 uint32_t lm_cnt_failed_size
;
43 uint32_t lm_cnt_failed_full
;
48 * Static initializer for global instances of logmem. Size order defines the
49 * total amount of logmem memory, the min and max order set the minimum and the
50 * maximum size respectively of the memory allocatable by the given logmem.
51 * Local or dynamically allocated instances of logmem should not be initialized
54 #define LOGMEM_STATIC_INIT(name, size_order, min_order, max_order) \
55 SIMPLE_LOCK_DECLARE(name##_lck, 0); \
57 .lm_lock = (lck_spin_t *)&name##_lck, \
58 .lm_mem = (uint8_t[(1 << (size_order))]){ 0 }, \
59 .lm_mem_map = (uint8_t[MAX(1, (1 << ((size_order) - (min_order) + 1)) / 8)]){ 0 }, \
60 .lm_cap_order = (size_order), \
61 .lm_max_order = (max_order), \
62 .lm_min_order = (min_order), \
63 .lm_cnt_free = (1 << (size_order)) \
67 * Allocates memory from a respective logmem. Returns a pointer to the beginning
68 * of the allocated block. The resulting size of the allocated block is equal or
69 * bigger than the size passed in during the call.
71 void *logmem_alloc(logmem_t
*, size_t *);
74 * Frees memory previously allocated by logmem_alloc(). The caller must call
75 * logmem_free() with exact pointer and size value returned by logmem_alloc().
77 void logmem_free(logmem_t
*, void *, size_t);
80 * Returns the maximum memory size allocatable by the logmem.
82 size_t logmem_max_size(const logmem_t
*);
84 #endif /* log_mem_h */