2 * rawbuffer.c: Manages buffers that might have been mmapped or malloced
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
12 #include <sys/types.h>
14 #include "rawbuffer.h"
18 GHashTable *malloc_map;
21 raw_buffer_load (int fd, int is_writable, guint32 base, size_t size)
33 start = base & ~(PAGESIZE - 1);
34 end = (base + size + PAGESIZE - 1) & ~(PAGESIZE - 1);
36 mmap_ptr = mmap (0, end - start, prot, flags, fd, start);
37 if (mmap_ptr == (void *) -1){
38 ptr = g_malloc (size);
41 if (lseek (fd, base, 0) == (off_t) -1)
46 if (malloc_map == NULL)
47 malloc_map = g_hash_table_new (g_direct_hash, g_direct_equal);
49 g_hash_table_insert (malloc_map, mmap_ptr, GINT_TO_POINTER (size));
51 return ((char *)mmap_ptr) + (base - start);
55 raw_buffer_free (void *buffer)
65 diff = ((unsigned int) buffer) & (PAGESIZE - 1);
66 base = ((char *)buffer) - diff;
68 size = GPOINTER_TO_INT (g_hash_table_lookup (malloc_map, base));