|
|
26e14f |
/***********************************************************/
|
|
|
26e14f |
/* ntux: native translation und extension */
|
|
|
db57dd |
/* Copyright (C) 2016--2018 Z. Gilboa */
|
|
|
26e14f |
/* Released under GPLv2 and GPLv3; see COPYING.NTUX. */
|
|
|
26e14f |
/***********************************************************/
|
|
|
26e14f |
|
|
|
26e14f |
#include <stddef.h>
|
|
|
26e14f |
#include <ntapi/ntapi.h>
|
|
|
26e14f |
#include "ntux_memfn_impl.h"
|
|
|
26e14f |
#include "ntux_driver_impl.h"
|
|
|
26e14f |
|
|
|
26e14f |
void * ntux_calloc(size_t n, size_t size)
|
|
|
26e14f |
{
|
|
|
26e14f |
struct ntux_memory_block block;
|
|
|
26e14f |
|
|
|
26e14f |
if (!n || (size > (size_t)(-1) / n))
|
|
|
26e14f |
return 0;
|
|
|
26e14f |
|
|
|
26e14f |
size *= n;
|
|
|
26e14f |
size += 0xf;
|
|
|
26e14f |
size &= ~(size_t)0xf;
|
|
|
26e14f |
|
|
|
26e14f |
block.addr = 0;
|
|
|
26e14f |
block.size = size + sizeof(block);
|
|
|
26e14f |
|
|
|
26e14f |
if (ntapi->zw_allocate_virtual_memory(
|
|
|
26e14f |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
26e14f |
&block.addr,
|
|
|
26e14f |
0,
|
|
|
26e14f |
&block.size,
|
|
|
26e14f |
NT_MEM_COMMIT,
|
|
|
26e14f |
NT_PAGE_READWRITE))
|
|
|
26e14f |
return 0;
|
|
|
26e14f |
|
|
|
26e14f |
block.used = size;
|
|
|
26e14f |
block.avail = block.size - block.used;
|
|
|
26e14f |
|
|
|
26e14f |
return (char *)block.addr + offsetof(struct ntux_memory_block,buffer);
|
|
|
26e14f |
}
|
|
|
26e14f |
|
|
|
26e14f |
void * ntux_balloc(
|
|
|
26e14f |
struct ntux_memory_block * cache,
|
|
|
26e14f |
struct ntux_memory_block * block,
|
|
|
26e14f |
size_t n, size_t size)
|
|
|
26e14f |
{
|
|
|
26e14f |
char * baddr;
|
|
|
26e14f |
void * addr = 0;
|
|
|
26e14f |
|
|
|
26e14f |
if (!n || (size > (size_t)(-1) / n))
|
|
|
26e14f |
return 0;
|
|
|
26e14f |
|
|
|
26e14f |
if (!cache || !cache->addr)
|
|
|
26e14f |
addr = ntux_calloc(n,size);
|
|
|
26e14f |
|
|
|
26e14f |
size *= n;
|
|
|
26e14f |
size += 0xf;
|
|
|
26e14f |
size &= ~(size_t)0xf;
|
|
|
26e14f |
|
|
|
26e14f |
if (size < block->avail)
|
|
|
26e14f |
addr = ntux_calloc(n,size);
|
|
|
26e14f |
|
|
|
26e14f |
/* newly allocated block? */
|
|
|
26e14f |
if (addr) {
|
|
|
26e14f |
baddr = addr;
|
|
|
26e14f |
baddr -= offsetof(struct ntux_memory_block,buffer);
|
|
|
26e14f |
|
|
|
26e14f |
ntapi->tt_aligned_block_memcpy(
|
|
|
26e14f |
(uintptr_t *)block,
|
|
|
26e14f |
(uintptr_t *)baddr,
|
|
|
26e14f |
sizeof(*block));
|
|
|
26e14f |
|
|
|
26e14f |
return addr;
|
|
|
26e14f |
}
|
|
|
26e14f |
|
|
|
26e14f |
/* cache */
|
|
|
26e14f |
addr = &block->buffer[block->used / sizeof(size_t)];
|
|
|
26e14f |
block->used += size;
|
|
|
26e14f |
block->avail -= size;
|
|
|
26e14f |
return addr;
|
|
|
26e14f |
}
|
|
|
26e14f |
|
|
|
26e14f |
void ntux_free(void * addr)
|
|
|
26e14f |
{
|
|
|
26e14f |
struct ntux_memory_block block;
|
|
|
26e14f |
|
|
|
26e14f |
block.addr = addr;
|
|
|
26e14f |
block.size = 0;
|
|
|
26e14f |
|
|
|
26e14f |
ntapi->zw_free_virtual_memory(
|
|
|
26e14f |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
26e14f |
&block.addr,
|
|
|
26e14f |
&block.size,
|
|
|
26e14f |
NT_MEM_RELEASE);
|
|
|
26e14f |
}
|