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