75 lines
2.0 KiB
C
75 lines
2.0 KiB
C
/* lwext4 freestanding allocation shim for NTLite drivers. */
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <ntlite/ddk/memory.h>
|
|
|
|
#include "ext4_port.h"
|
|
|
|
void* ext4_ntl_malloc(size_t size) {
|
|
if (size == 0)
|
|
size = 1;
|
|
return MmAllocatePool(size);
|
|
}
|
|
|
|
void* ext4_ntl_calloc(size_t count, size_t size) {
|
|
if (count != 0 && size > SIZE_MAX / count)
|
|
return (void*)0;
|
|
|
|
size_t bytes = count * size;
|
|
if (bytes == 0)
|
|
bytes = 1;
|
|
|
|
void* ptr = MmAllocatePool(bytes);
|
|
if (ptr)
|
|
memset(ptr, 0, bytes);
|
|
return ptr;
|
|
}
|
|
|
|
void* ext4_ntl_realloc(void* ptr, size_t size) {
|
|
if (!ptr)
|
|
return ext4_ntl_malloc(size);
|
|
if (size == 0) {
|
|
MmFreePool(ptr);
|
|
return (void*)0;
|
|
}
|
|
return MmReallocatePool(ptr, size);
|
|
}
|
|
|
|
void ext4_ntl_free(void* ptr) {
|
|
if (ptr)
|
|
MmFreePool(ptr);
|
|
}
|
|
|
|
/* lwext4 uses the standard allocation names when CONFIG_USE_USER_MALLOC=0. */
|
|
void* malloc(size_t size) { return ext4_ntl_malloc(size); }
|
|
void* calloc(size_t count, size_t size) { return ext4_ntl_calloc(count, size); }
|
|
void* realloc(void* ptr, size_t size) { return ext4_ntl_realloc(ptr, size); }
|
|
void free(void* ptr) { ext4_ntl_free(ptr); }
|
|
/* lwext4 uses qsort for directory-index construction. A small insertion
|
|
* sort is sufficient here and keeps the freestanding port dependency-free. */
|
|
void qsort(void* base, size_t count, size_t size,
|
|
int (*compare)(const void*, const void*)) {
|
|
if (!base || !compare || size == 0 || count < 2)
|
|
return;
|
|
|
|
unsigned char* bytes = (unsigned char*)base;
|
|
for (size_t i = 1; i < count; ++i) {
|
|
size_t j = i;
|
|
while (j != 0) {
|
|
unsigned char* left = bytes + (j - 1) * size;
|
|
unsigned char* right = bytes + j * size;
|
|
if (compare(left, right) <= 0)
|
|
break;
|
|
|
|
for (size_t k = 0; k < size; ++k) {
|
|
unsigned char temp = left[k];
|
|
left[k] = right[k];
|
|
right[k] = temp;
|
|
}
|
|
--j;
|
|
}
|
|
}
|
|
}
|