xref: /xnu-12377.61.12/tests/unit/mocks/mock_mem.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include "std_safe.h"
30 #include "dt_proxy.h"
31 #include "unit_test_utils.h"
32 
33 /* This is an implementation of simple fixed size same-size objects pool */
34 struct mock_mem_pool {
35 	size_t elem_size;
36 	char *buffer;
37 	char *free_head;
38 	uint32_t free_count;
39 };
40 
41 void
mock_mem_init(struct mock_mem_pool * mm,size_t elem_sz,uint32_t count)42 mock_mem_init(struct mock_mem_pool* mm, size_t elem_sz, uint32_t count)
43 {
44 	mm->elem_size = elem_sz;
45 	size_t buf_size = elem_sz * count;
46 	mm->buffer = aligned_alloc(8, buf_size);
47 	PT_QUIET; PT_ASSERT_NOTNULL(mm->buffer, "failed alloc");
48 	memset(mm->buffer, 0, buf_size);
49 	mm->free_head = mm->buffer;
50 	mm->free_count = count;
51 }
52 
53 void *
mock_mem_alloc(struct mock_mem_pool * mm)54 mock_mem_alloc(struct mock_mem_pool* mm)
55 {
56 	PT_QUIET; PT_ASSERT_NOTNULL(mm->buffer, "mock mem not allocated");
57 	PT_QUIET; PT_ASSERT_TRUE(mm->free_count > 0, "no more space left");
58 	void *ret = mm->free_head;
59 	mm->free_head += mm->elem_size;
60 	mm->free_count--;
61 	return ret;
62 }
63 
64 void
mock_mem_free(struct mock_mem_pool * mm,void * ptr)65 mock_mem_free(struct mock_mem_pool* mm, void *ptr)
66 {
67 	// not implemeted yet rdar://136915968
68 }
69 
70 struct mock_mem_pool mm_vm_objects;
71 
72 
73 // this is used for vm_object and vm_page pointer packing
74 uintptr_t mock_page_ptr_base;
75 
76 void
mock_mem_init_vm_objects(void)77 mock_mem_init_vm_objects(void)
78 {
79 	mock_mem_init(&mm_vm_objects, 256, 100);
80 	mock_page_ptr_base = (uintptr_t)mm_vm_objects.buffer;
81 }
82 void *
mock_mem_alloc_vm_object(void)83 mock_mem_alloc_vm_object(void)
84 {
85 	return mock_mem_alloc(&mm_vm_objects);
86 }
87