1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at>                 |
| All rights reserved.                                                       |
|                                                                            |
| Redistribution and use in source and binary forms, with or without         |
| modification, are permitted provided that the following conditions are met:|
|                                                                            |
| 1. Redistributions of source code must retain the above copyright notice,  |
|    this list of conditions and the following disclaimer.                   |
| 2. Redistributions in binary form must reproduce the above copyright       |
|    notice, this list of conditions and the following disclaimer in the     |
|    documentation and/or other materials provided with the distribution.    |
|                                                                            |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE    |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR        |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF       |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN    |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)    |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGE.                                            |
+----------------------------------------------------------------------------+
*/

#include "gate/memalloc.h"
#include "gate/gatetypes.h"
#include "gate/debugging.h"
#include "gate/results.h"

#if defined(GATE_SYS_EFI)
#	define GATE_MEMALLOC_ALLOC_STD_IMPL
#	define GATE_MEMALLOC_ACCESS_INTERNAL_IMPL
#else
#	define GATE_MEMALLOC_ALLOC_STD_IMPL
#	define GATE_MEMALLOC_ACCESS_STD_IMPL
#endif


#if defined(GATE_MEMALLOC_ALLOC_STD_IMPL)

/* memory allocation C standard implementation */

#include <stdlib.h>
#include <string.h>

void* gate_mem_alloc(gate_size_t sz)
{
    GATE_DEBUG_ASSERT(sz != 0);
    return malloc(sz);
}
void* gate_mem_realloc(void* ptr, gate_size_t newsize)
{
    GATE_DEBUG_ASSERT(newsize != 0);
    return realloc(ptr, newsize);
}
void gate_mem_dealloc(void* ptr)
{
    GATE_DEBUG_ASSERT(ptr != NULL);
    free(ptr);
}

#endif /* GATE_MEMALLOC_ALLOC_STD_IMPL */





#if defined(GATE_MEMALLOC_ACCESS_STD_IMPL)

/* memory access C standard implementation */

#include <stdlib.h>
#include <string.h>

void* gate_mem_copy(void* dst, void const* src, gate_size_t sz)
{
    GATE_DEBUG_ASSERT((dst != NULL) && (src != NULL) && (sz != 0));
    return memcpy(dst, src, sz);
}

void* gate_mem_move(void* dst, void const* src, gate_size_t sz)
{
    GATE_DEBUG_ASSERT((dst != NULL) && (src != NULL) && (sz != 0));
    return memmove(dst, src, sz);
}

void gate_mem_clear(void* dst, gate_size_t sz)
{
    memset(dst, 0, sz);
}

void gate_mem_fill(void* dst, char chr, gate_size_t count)
{
    memset(dst, (unsigned char)chr, count);
}

int gate_mem_compare(void const* ptr1, void const* ptr2, gate_size_t sz)
{
    return (int)memcmp(ptr1, ptr2, sz);
}

#endif /* GATE_MEMALLOC_MOVE_STD_IMPL */


#if defined(GATE_MEMALLOC_ACCESS_INTERNAL_IMPL)

/* internal direct byte access implementation */

void* gate_mem_copy(void* dst, void const* src, gate_size_t sz)
{
    char* ptr_dst = (char*)dst;
    char const* ptr_src = (char const*)src;
    GATE_DEBUG_ASSERT((dst != NULL) && (src != NULL) && (sz != 0));

    while (sz-- != 0)
    {
        *ptr_dst = *ptr_src;
        ++ptr_dst;
        ++ptr_src;
    }
    return dst;
}

void* gate_mem_move(void* dst, void const* src, gate_size_t sz)
{
    char* ptr_dst = (char*)dst;
    char const* ptr_src = (char const*)src;
    GATE_DEBUG_ASSERT((dst != NULL) && (src != NULL) && (sz != 0));

    if ((ptr_dst <= ptr_src) || (ptr_src + sz < ptr_dst))
    {
        return gate_mem_copy(dst, src, sz);
    }
    else
    {
        ptr_dst += sz;
        ptr_src += sz;
        while (sz-- != 0)
        {
            --ptr_dst;
            --ptr_src;
            *ptr_dst = *ptr_src;
        }
        return dst;
    }
}

void gate_mem_clear(void* dst, gate_size_t count)
{
    gate_mem_fill(dst, 0, count);
}

void gate_mem_fill(void* dst, char chr, gate_size_t count)
{
    char* ptr = (char*)dst;
    while (count-- != 0)
    {
        *ptr = chr;
        ++ptr;
    }
}


int gate_mem_compare(void const* ptr1, void const* ptr2, gate_size_t sz)
{
    unsigned char const* p1 = (unsigned char const*)ptr1;
    unsigned char const* p2 = (unsigned char const*)ptr2;
    while (sz-- != 0)
    {
        if (*p1 < *p2)
        {
            return -1;
        }
        if (*p1 > *p2)
        {
            return 1;
        }
        ++p1;
        ++p2;
    }
    return 0;
}

#endif /* GATE_MEMALLOC_ACCESS_INTERNAL_IMPL */






/***************************
 * Generic implementations *
 ***************************/

gate_size_t gate_mem_align_size(gate_size_t sz)
{
    gate_size_t m = sz % sizeof(gate_c_maxalign_t);
    if (m != 0)
    {
        sz += (sizeof(gate_c_maxalign_t) - m);
    }
    return sz;
}
void gate_mem_reversebyteorder(void* ptr, gate_size_t sz)
{
    gate_size_t cnt = sz / 2;
    char* ptr1 = (char*)ptr;
    char* ptr2 = ptr1 + sz - 1;
    char tmp;
    while (cnt-- != 0)
    {
        tmp = *ptr1;
        *ptr1 = *ptr2;
        *ptr2 = tmp;
        ++ptr1;
        --ptr2;
    }
}
void* gate_mem_copy_reverse(void* dst, void const* src, gate_size_t sz)
{
    char* ptrdst = (char*)dst;
    char const* ptrsrc = (char const*)src + sz;
    while (sz-- != 0)
    {
        --ptrsrc;
        *ptrdst = *ptrsrc;
        ++ptrdst;
    }
    return dst;
}

gate_result_t gate_mem_copy_construct(void* dest_item, void const* src_item, gate_size_t item_size, gate_mem_copyctor_t ctor)
{
    if (ctor == NULL)
    {
        gate_mem_copy(dest_item, src_item, item_size);
        return GATE_RESULT_OK;
    }
    else
    {
        return ctor(dest_item, src_item);
    }
}

void gate_mem_destruct(void* dest_item, gate_mem_dtor_t dtor)
{
    if (dtor != NULL)
    {
        dtor(dest_item);
    }
}
gate_result_t gate_mem_move_construct(void* dest_item, void* src_item, gate_size_t item_size, gate_mem_copyctor_t ctor, gate_mem_dtor_t dtor)
{
    gate_result_t result;
    if (dest_item == src_item)
    {
        return GATE_RESULT_OK;
    }
    result = gate_mem_copy_construct(dest_item, src_item, item_size, ctor);
    if (GATE_SUCCEEDED(result))
    {
        gate_mem_destruct(src_item, dtor);
    }
    return result;
}


gate_bool_t gate_mem_swap(void* item1, void* item2, gate_size_t item_size, gate_mem_copyctor_t copy_constructor, gate_mem_dtor_t destructor)
{
    gate_result_t result;
    gate_bool_t ret = false;
    char buffer[GATE_MAX_COPYBUFFER_LENGTH];
    char* ptr_buffer = &buffer[0];

    do
    {
        if (item1 == item2)
        {
            /* pointers are equal -> nothing to swap */
            ret = true;
            break;
        }
        if (item_size > sizeof(buffer))
        {
            ptr_buffer = gate_mem_alloc(item_size);
            if (ptr_buffer == NULL)
            {
                ptr_buffer = &buffer[0];
                break;
            }
        }

        result = gate_mem_copy_construct(ptr_buffer, item1, item_size, copy_constructor);
        GATE_BREAK_IF_FAILED(result);

        gate_mem_destruct(item1, destructor);
        result = gate_mem_copy_construct(item1, item2, item_size, copy_constructor);
        if (GATE_FAILED(result))
        {
            gate_mem_copy_construct(item1, ptr_buffer, item_size, copy_constructor);
            gate_mem_destruct(ptr_buffer, destructor);
            break;
        }

        gate_mem_destruct(item2, destructor);
        result = gate_mem_copy_construct(item2, ptr_buffer, item_size, copy_constructor);
        if (GATE_FAILED(result))
        {
            gate_mem_copy_construct(item2, item1, item_size, copy_constructor);
            gate_mem_destruct(item1, destructor);
            gate_mem_copy_construct(item1, ptr_buffer, item_size, copy_constructor);
            gate_mem_destruct(ptr_buffer, destructor);
            break;
        }

        gate_mem_destruct(ptr_buffer, destructor);
        ret = true;
    } while (0);

    if (ptr_buffer != &buffer[0])
    {
        gate_mem_dealloc(ptr_buffer);
    }
    return ret;
}

static gate_bool_t is_pointer_aligned(void const* ptr)
{
    const gate_uintptr_t addr = (gate_uintptr_t)ptr;
    return (addr % sizeof(void*) == 0);
}

void gate_mem_swap_bytes(void* mem1, void* mem2, gate_size_t length)
{
    gate_size_t bytelen = length % sizeof(void*);
    gate_uint8_t* b1;
    gate_uint8_t* b2;

    if (is_pointer_aligned(mem1) && is_pointer_aligned(mem2))
    {
        /* faster transfer using aligned pointer-swap */
        void** v1 = (void**)mem1;
        void** v2 = (void**)mem2;
        gate_size_t reglen = length / sizeof(void*);
        const gate_size_t skiplen = reglen * sizeof(void*);

        for (; reglen != 0; --reglen, ++v1, ++v2)
        {
            void* const tmp = *v1;
            *v1 = *v2;
            *v2 = tmp;
        }

        length -= skiplen;
        mem1 = (void*)((char*)mem1 + skiplen);
        mem2 = (void*)((char*)mem2 + skiplen);
    }

    b1 = (gate_uint8_t*)mem1;
    b2 = (gate_uint8_t*)mem2;
    for (; bytelen != 0; --bytelen, ++b1, ++b2)
    {
        const gate_uint8_t tmp = *b1;
        *b1 = *b2;
        *b2 = tmp;
    }
}