polar-objc 5.44
An Objective-C runtime library
Loading...
Searching...
No Matches
polar-memory-pool-page.c
1/* Internal API: memory pool page type for polar-objc.
2 Copyright (C) 2022-2025 Michael Malicoat <[email protected]>
3
4 This file is part of polar-objc.
5
6 polar-objc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
7 License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
8
9 polar-objc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 details.
12
13 You should have received a copy of the GNU General Public License along with this program; see the file LICENSE. If
14 not, see <http://www.gnu.org/licenses/>.
15*/
16// polar_memory_pool_page **********************************************************************************************
17// Private =============================================================================================================
18
19// This variable is initialized in polar_memory_pool_page_get_type()
20static polar_memory_pool_page_class class_polar_memory_pool_page;
21
22// This variable is initialized in _polar_memory_pool_page_class_init()
23static polar_runtime_object_class *superclass_polar_memory_pool_page = NULL;
24
25// ---------------------------------------------------------------------------------------------------------------------
26
27POLAR_FUNCTION_INTERNAL polar_memory_pool_page *
28polar_memory_pool_page_allocate_sized( polar_memory_pool_page_class *self, size_t size_page_desired )
29{
31 size_t size_type_instance, size_page_allocated;
32
33 assert( POLAR_IS_MEMORY_POOL_PAGE_CLASS(self) );
34
35 size_type_instance = POLAR_RUNTIME_OBJECT_CLASS(self)->type_definition->size_type_instance;
36
37 if( size_page_desired != (size_t)-1 )
38 {
39 assert( size_page_desired >= size_type_instance );
40 }
41
42 else
43 size_page_desired = size_type_instance;
44
45 result = NULL;
46 size_page_allocated = polar_platform_vmem_map( &result, (size_t)size_page_desired, NO );
47
48 if( result != NULL )
49 {
50 result->page_size = size_page_allocated;
51 polar_runtime_object_preallocated_init(result, POLAR_RUNTIME_OBJECT_CLASS(self)->id_type_class, NO);
52 }
53
54 else
55 POLAR_RUNTIME_ERROR->memory_exhausted(POLAR_RUNTIME_OBJECT_CLASS(self)->type_definition->size_type_instance);
56
57 return result;
58}
59
60// ---------------------------------------------------------------------------------------------------------------------
61
62POLAR_FUNCTION_INTERNAL polar_memory_pool_page *
63_polar_memory_pool_page_init( polar_memory_pool_page *self )
64{
65 size_t size_instance;
66
67 // Chain up first
68 self = (polar_memory_pool_page *)
69 POLAR_RUNTIME_OBJECT_CLASS(superclass_polar_memory_pool_page)->init( (polar_runtime_object *)self );
70
71 if( self != NULL )
72 {
73 size_instance = polar_runtime_object_get_size((polar_runtime_object *)self);
74 self->page_break = ( (char *)self + OBJC_SIZE_ALIGN(size_instance, OBJC_SIZEOF_POINTER) );
75 }
76
77 return self;
78}
79
80POLAR_FUNCTION_INTERNAL void
81_polar_memory_pool_page_deallocate( polar_memory_pool_page *self )
82{
83 polar_platform_vmem_unmap(self, self->page_size);
84}
85
86POLAR_FUNCTION_INTERNAL polar_memory_pool_page *
87_polar_memory_pool_page_allocate( polar_memory_pool_page_class *self )
88{
89 return polar_memory_pool_page_allocate_sized(self, (size_t)-1);
90}
91
92POLAR_FUNCTION_INTERNAL void
93_polar_memory_pool_page_clear( polar_memory_pool_page *self )
94{
95 size_t count_bytes_committed_old, size_instance;
96
97 size_instance = polar_runtime_object_get_size( (polar_runtime_object *)self );
98 size_instance = OBJC_SIZE_ALIGN(size_instance, OBJC_SIZEOF_POINTER);
99
100 count_bytes_committed_old = polar_memory_pool_page_get_bytes_committed(self) - size_instance;
101 self->page_break = ( (char *)self + size_instance );
102
103 objc_memzero( self->page_break, count_bytes_committed_old );
104}
105
106POLAR_FUNCTION_INTERNAL void
107_polar_memory_pool_page_class_init( polar_memory_pool_page_class *self )
108{
109 // Acquire our superclass
110 superclass_polar_memory_pool_page = POLAR_RUNTIME_OBJECT_CLASS(self)->class_superclass;
111
112 // Establish our virtual methods
113 ( (polar_runtime_object_class *)self )->init = (polar_func_runtime_object_lifecycle)_polar_memory_pool_page_init;
114
115 ( (polar_runtime_object_class *)self )->deallocate =
116 (polar_func_runtime_object_deallocate)_polar_memory_pool_page_deallocate;
117
118 ( (polar_runtime_object_class *)self )->allocate =
119 (polar_func_runtime_object_allocate)_polar_memory_pool_page_allocate;
120
121 self->clear = _polar_memory_pool_page_clear;
122}
123
124static struct polar_internal_type_info pti_polar_memory_pool_page =
125{
126 .name_type = "polar_memory_pool_page",
127 .size_type_instance = sizeof(polar_memory_pool_page),
128 .size_type_class = sizeof(polar_memory_pool_page_class),
129 .type_class_init = (polar_func_runtime_object_class_lifecycle)_polar_memory_pool_page_class_init
130};
131
132// Public ==============================================================================================================
133
134POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_CONSTANT polar_internal_type
135polar_memory_pool_page_get_type( void )
136{
137 static polar_internal_type pt_polar_memory_pool_page = POLAR_INTERNAL_TYPE_INVALID;
138
139 if( pt_polar_memory_pool_page != POLAR_INTERNAL_TYPE_INVALID )
140 return pt_polar_memory_pool_page;
141
142 pt_polar_memory_pool_page = polar_internal_type_init( POLAR_TYPE_LIST_NODE, &class_polar_memory_pool_page,
143 &pti_polar_memory_pool_page );
144
145 return pt_polar_memory_pool_page;
146}
147
148POLAR_FUNCTION_INTERNAL OBJC_RETURNS_VALID_POINTER polar_memory_pool_page *
149polar_memory_pool_page_new( size_t size_page_desired )
150{
152
153 assert( size_page_desired >= sizeof(polar_memory_pool_page) );
154
155 type_class = (polar_memory_pool_page_class *)polar_internal_type_get_class(POLAR_TYPE_MEMORY_POOL_PAGE);
156 return polar_memory_pool_page_allocate_sized(type_class, size_page_desired );
157}
158
159POLAR_FUNCTION_INTERNAL OBJC_FUNCTION_HOTSPOT void *
160polar_memory_pool_page_sbrk( polar_memory_pool_page *self, size_t count_bytes_increment )
161{
162 char *result;
163
164 assert( POLAR_IS_MEMORY_POOL_PAGE(self) );
165 assert( count_bytes_increment > 0 );
166
167 result = NULL;
168
169 if( polar_memory_pool_page_test_fit(self, count_bytes_increment) )
170 {
171 result = self->page_break;
172 self->page_break += count_bytes_increment;
173 }
174
175 return result;
176}
static void polar_platform_vmem_unmap(void *p_page, size_t count_bytes)
Definition polar-windows-platform-vmem.h:43
size_t polar_platform_vmem_map(void *out_page_first, size_t count_bytes, BOOL flag_executable)
Definition polar-windows-platform-vmem.c:26
Definition polar-internal-type.h:34
Definition polar-memory-pool-page.h:31
Definition polar-memory-pool-page.h:24
Definition polar-runtime-object.h:36
Definition polar-runtime-object.h:31