2 * reactos memory pools emulation of libcaptive
3 * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "captive/mm.h" /* self */
23 #include "reactos/internal/mm.h" /* self */
24 #include "reactos/ntos/types.h" /* for PVOID etc. */
25 #include <glib/gmessages.h>
26 #include <glib/gmem.h>
27 #include <glib/ghash.h>
30 /* map PVOID->GUINT_TO_POINTER(ULONG Tag) */
31 static GHashTable *memory_Tag_hash;
33 static void memory_Tag_hash_init(void)
37 memory_Tag_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
42 * ExAllocatePoolWithTag:
43 * @PoolType: Type of memory to allocate. Safely ignored by libcaptive as it is
44 * too low level for it.
45 * @NumberOfBytes: Size of the requested allocation. Value 0 permitted (returns %NULL).
46 * @Tag: 4-bytes of block identification for debugging purposes and/or ExFreePoolWithTag().
47 * Bit 7 (==%0x80 ) of each byte must be cleared.
48 * %PROTECTED_POOL with @Tag bit 31 set is not yet implemented.
50 * Allocates the specified memory block. libcaptive passes the allocation to
51 * g_malloc() and @Tag is marked to this allocation.
53 * Returns: Memory block base if successfuly allocated. %NULL otherwise.
54 * The allocated memory block is not cleared.
56 PVOID ExAllocatePoolWithTag(ULONG PoolType,ULONG NumberOfBytes,ULONG Tag)
60 g_return_val_if_fail((Tag&0x80808080)==0,NULL);
62 memory_Tag_hash_init();
66 /* FIXME: >=PAGE_SIZE allocations should be PAGE_SIZE aligned */
67 r=g_malloc(NumberOfBytes);
69 g_assert(FALSE==g_hash_table_lookup_extended(memory_Tag_hash,
73 g_hash_table_insert(memory_Tag_hash,
74 (gpointer)r, /* key */
75 GUINT_TO_POINTER(Tag)); /* value */
82 * @PoolType: Type of memory to allocate. Safely ignored by libcaptive as it is
83 * too low level for it.
84 * @NumberOfBytes: Size of the requested allocation. Value 0 permitted (returns %NULL).
86 * Allocates the specified memory block. Calls ExAllocatePoolWithTag() with
87 * @Tag value %0x00000000. At least this value seems to be assumed by WXP <ntddk.h>
88 * although it is not officially documented for W32.
90 * Returns: Memory block base if successfuly allocated. %NULL otherwise.
91 * The allocated memory block is not cleared.
93 PVOID ExAllocatePool(POOL_TYPE PoolType,ULONG NumberOfBytes)
95 /* FIXME: It would be better to mark such block as 'unTagged' but we would have
96 * to maintain some structure for 'memory_Tag_hash' values instead of just GUINT_TO_POINTER().
98 return ExAllocatePoolWithTag(PoolType,NumberOfBytes,
99 0x00000000); /* Tag; reactos uses TAG_NONE ('None') but it W32 uses %0x00000000 */
104 * @Block: Base address of the memory block. %NULL value is forbidden.
106 * Deallocate the given memory block. Block must be already successfuly
107 * allocated by a previous ExAllocatePool() or ExAllocatePoolWithTag() call.
108 * You can no longer assume anything about this base address / memory block.
110 VOID ExFreePool(IN PVOID Block)
114 g_return_if_fail(Block!=NULL);
116 memory_Tag_hash_init();
118 errbool=g_hash_table_remove(memory_Tag_hash,Block);
119 g_assert(errbool==TRUE);
127 * @Block: Base address of the memory block. %NULL value is forbidden.
128 * @Tag: 4-bytes of required block identification.
129 * Bit 7 (==%0x80 ) of each byte must be cleared.
130 * %PROTECTED_POOL with @Tag bit 31 set is not yet implemented.
131 * Value %0x0000000 is permitted for blocks marked with any Tag.
133 * Deallocate the given memory block. Block must be already successfuly
134 * allocated by a previous ExAllocatePoolWithTag() call.
135 * Block may be allocated by ExAllocatePool() if @Tag is passed %0x00000000.
136 * Please see more about @Tag in ExAllocatePool().
137 * It is forbidden to pass invalid @Tag for @Block.
139 * You can no longer assume anything about this base address / memory block.
141 VOID ExFreePoolWithTag(IN PVOID Block,IN ULONG Tag)
143 gpointer memory_Tag_gpointer;
146 g_return_if_fail(Block!=NULL);
147 g_return_if_fail((Tag&0x80808080)==0);
149 memory_Tag_hash_init();
151 errbool=g_hash_table_lookup_extended(memory_Tag_hash,
152 Block, /* lookup_key */
154 &memory_Tag_gpointer); /* value */
155 g_return_if_fail(errbool==TRUE);
156 g_return_if_fail(!Tag || GPOINTER_TO_UINT(memory_Tag_gpointer)==Tag);