|
|
0efa8c |
/*********************************************************/
|
|
|
0efa8c |
/* ptycon: a pty-console bridge */
|
|
|
1f983d |
/* Copyright (C) 2016--2017 Z. Gilboa */
|
|
|
0efa8c |
/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */
|
|
|
0efa8c |
/*********************************************************/
|
|
|
0efa8c |
|
|
|
0efa8c |
#include <stddef.h>
|
|
|
0efa8c |
#include <ntapi/ntapi.h>
|
|
|
0efa8c |
#include "ptycon_memfn_impl.h"
|
|
|
0efa8c |
#include "ptycon_driver_impl.h"
|
|
|
0efa8c |
|
|
|
0efa8c |
void * ptyc_calloc(size_t n, size_t size)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct ptyc_memory_block block;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!n || (size > (size_t)(-1) / n))
|
|
|
0efa8c |
return 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
size *= n;
|
|
|
0efa8c |
size += 0xf;
|
|
|
0efa8c |
size &= ~(size_t)0xf;
|
|
|
0efa8c |
|
|
|
0efa8c |
block.addr = 0;
|
|
|
0efa8c |
block.size = size + sizeof(block);
|
|
|
0efa8c |
|
|
|
0efa8c |
if (ntapi->zw_allocate_virtual_memory(
|
|
|
0efa8c |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
0efa8c |
&block.addr,
|
|
|
0efa8c |
0,
|
|
|
0efa8c |
&block.size,
|
|
|
0efa8c |
NT_MEM_COMMIT,
|
|
|
0efa8c |
NT_PAGE_READWRITE))
|
|
|
0efa8c |
return 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
block.used = size;
|
|
|
0efa8c |
block.avail = block.size - block.used;
|
|
|
0efa8c |
|
|
|
0efa8c |
return (char *)block.addr + offsetof(struct ptyc_memory_block,buffer);
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
void * ptyc_balloc(
|
|
|
0efa8c |
struct ptyc_memory_block * cache,
|
|
|
0efa8c |
struct ptyc_memory_block * block,
|
|
|
0efa8c |
size_t n, size_t size)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
char * baddr;
|
|
|
0efa8c |
void * addr = 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!n || (size > (size_t)(-1) / n))
|
|
|
0efa8c |
return 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (!cache || !cache->addr)
|
|
|
0efa8c |
addr = ptyc_calloc(n,size);
|
|
|
0efa8c |
|
|
|
0efa8c |
size *= n;
|
|
|
0efa8c |
size += 0xf;
|
|
|
0efa8c |
size &= ~(size_t)0xf;
|
|
|
0efa8c |
|
|
|
0efa8c |
if (size < block->avail)
|
|
|
0efa8c |
addr = ptyc_calloc(n,size);
|
|
|
0efa8c |
|
|
|
0efa8c |
/* newly allocated block? */
|
|
|
0efa8c |
if (addr) {
|
|
|
0efa8c |
baddr = addr;
|
|
|
0efa8c |
baddr -= offsetof(struct ptyc_memory_block,buffer);
|
|
|
0efa8c |
|
|
|
0efa8c |
ntapi->tt_aligned_block_memcpy(
|
|
|
0efa8c |
(uintptr_t *)block,
|
|
|
0efa8c |
(uintptr_t *)baddr,
|
|
|
0efa8c |
sizeof(*block));
|
|
|
0efa8c |
|
|
|
0efa8c |
return addr;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
/* cache */
|
|
|
0efa8c |
addr = &block->buffer[block->used / sizeof(size_t)];
|
|
|
0efa8c |
block->used += size;
|
|
|
0efa8c |
block->avail -= size;
|
|
|
0efa8c |
return addr;
|
|
|
0efa8c |
}
|
|
|
0efa8c |
|
|
|
0efa8c |
void ptyc_free(void * addr)
|
|
|
0efa8c |
{
|
|
|
0efa8c |
struct ptyc_memory_block block;
|
|
|
0efa8c |
|
|
|
0efa8c |
block.addr = addr;
|
|
|
0efa8c |
block.size = 0;
|
|
|
0efa8c |
|
|
|
0efa8c |
ntapi->zw_free_virtual_memory(
|
|
|
0efa8c |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
0efa8c |
&block.addr,
|
|
|
0efa8c |
&block.size,
|
|
|
0efa8c |
NT_MEM_RELEASE);
|
|
|
0efa8c |
}
|