aboutsummaryrefslogtreecommitdiffstats
path: root/zalloc.c
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2001-10-20 02:12:22 +0000
committerH. Peter Anvin <hpa@zytor.com>2001-10-20 02:12:22 +0000
commit50cedb0ff19233273ef33cb5b5e8725025a35024 (patch)
tree25b21afff2ef45d706df0b05f18473516b8911f5 /zalloc.c
parentf3d637296f1f839d4a95a849a7dbfbb600f563f1 (diff)
downloadlpsm-50cedb0ff19233273ef33cb5b5e8725025a35024.tar.gz
lpsm-50cedb0ff19233273ef33cb5b5e8725025a35024.tar.xz
lpsm-50cedb0ff19233273ef33cb5b5e8725025a35024.zip
Split apart into modules. Create lpsm_zalloc()/lpsm_calloc().
Diffstat (limited to 'zalloc.c')
-rw-r--r--zalloc.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/zalloc.c b/zalloc.c
new file mode 100644
index 0000000..b6e5195
--- /dev/null
+++ b/zalloc.c
@@ -0,0 +1,72 @@
+#ident "$Id$"
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2000-2001 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, Inc.,
+ * 59 Temple Place Ste 330, Boston MA 02111-1307, USA, version 2.1,
+ * incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * zalloc.c
+ *
+ * Allocate zero-initialized memory. Try to be efficient if the area is
+ * large.
+ *
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/mman.h>
+#include "lpsm.h"
+#include "internals.h"
+
+void *lpsm_zalloc(size_t size)
+{
+ void *ptr, *mv;
+ int xbit, obit, rorder;
+ int order = BUDDY_ORDER_MIN;
+
+ if ( size > AH->slab[0].size ) {
+ while ( size > ((size_t)1 << order) )
+ order++;
+
+ xbit = lpsm_malloc_buddy_xbit(size);
+ if ( !xbit ) {
+ ptr = NULL;
+ } else {
+ rorder = AH->arena_size_lg2 - order;
+ obit = 1 << rorder;
+ ptr = (void *)((char *)AH->data_start +
+ ((uintptr_t)(xbit-obit) << order));
+ if ( (uintptr_t)ptr & (PM->pagesize-1) ||
+ order < PM->pageshift ) {
+ memset(ptr, 0, size); /* Misaligned or too small */
+ } else {
+ size = (size_t)1 << order; /* "Real" size */
+ /* Do an anonymous mmap() over the affected area */
+ mv = mmap(ptr, size, PROT_READ|PROT_WRITE,
+ MAP_FIXED|MAP_PRIVATE, 0, 0);
+ if ( mv != ptr ) {
+ memset(ptr, 0, size);
+ } else {
+ /* Mark dirty */
+ memset(PM->pageinfo + ((ptr-PM->arena) >> PM->pageshift),
+ page_dirty, size >> PM->pageshift);
+ }
+ }
+ }
+ } else {
+ ptr = lpsm_malloc_slab(size);
+ if ( ptr )
+ memset(ptr, 0, size);
+ }
+
+ return ptr;
+}
+