]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2020 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
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 | |
11 | * file. | |
12 | * | |
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. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #ifndef log_mem_h | |
25 | #define log_mem_h | |
26 | ||
27 | #include <stddef.h> | |
28 | #include <stdint.h> | |
29 | ||
30 | /* | |
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. | |
33 | */ | |
34 | typedef struct logmem_s { | |
35 | lck_spin_t *lm_lock; | |
36 | uint8_t *lm_mem; | |
37 | uint8_t *lm_mem_map; | |
38 | size_t lm_cap_order; | |
39 | size_t lm_min_order; | |
40 | size_t lm_max_order; | |
41 | uint32_t lm_cnt_allocations; | |
42 | uint32_t lm_cnt_failed_size; | |
43 | uint32_t lm_cnt_failed_full; | |
44 | uint32_t lm_cnt_free; | |
45 | } logmem_t; | |
46 | ||
47 | /* | |
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 | |
52 | * by this macro. | |
53 | */ | |
54 | #define LOGMEM_STATIC_INIT(name, size_order, min_order, max_order) \ | |
55 | SIMPLE_LOCK_DECLARE(name##_lck, 0); \ | |
56 | logmem_t name = { \ | |
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)) \ | |
64 | }; | |
65 | ||
66 | /* | |
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. | |
70 | */ | |
71 | void *logmem_alloc(logmem_t *, size_t *); | |
72 | ||
73 | /* | |
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(). | |
76 | */ | |
77 | void logmem_free(logmem_t *, void *, size_t); | |
78 | ||
79 | /* | |
80 | * Returns the maximum memory size allocatable by the logmem. | |
81 | */ | |
82 | size_t logmem_max_size(const logmem_t *); | |
83 | ||
84 | #endif /* log_mem_h */ |