summaryrefslogtreecommitdiff
path: root/src/lib/libast/astsa/vmalloc.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
committerIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
commit3950ffe2a485479f6561c27364d3d7df5a21d124 (patch)
tree468c6e14449d1b1e279222ec32f676b0311917d2 /src/lib/libast/astsa/vmalloc.c
downloadksh-upstream.tar.gz
Imported Upstream version 93u+upstream
Diffstat (limited to 'src/lib/libast/astsa/vmalloc.c')
-rw-r--r--src/lib/libast/astsa/vmalloc.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/lib/libast/astsa/vmalloc.c b/src/lib/libast/astsa/vmalloc.c
new file mode 100644
index 0000000..3195b13
--- /dev/null
+++ b/src/lib/libast/astsa/vmalloc.c
@@ -0,0 +1,102 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+/*
+ * standalone mini vmalloc implementation
+ * no resize, no free, no disciplines, no methods
+ */
+
+#include <ast.h>
+#include <vmalloc.h>
+
+Vmalloc_t* Vmregion;
+
+Vmalloc_t*
+_vm_open(void)
+{
+ Vmalloc_t* vp;
+
+ if (vp = newof(0, Vmalloc_t, 1, 0))
+ {
+ vp->current = &vp->base;
+ vp->data = vp->current->data;
+ vp->size = sizeof(vp->current->data);
+ }
+ return vp;
+}
+
+int
+_vm_close(register Vmalloc_t* vp)
+{
+ register Vmchunk_t* cp;
+ register Vmchunk_t* np;
+
+ if (!vp)
+ return -1;
+ np = vp->base.next;
+ while (cp = np)
+ {
+ np = cp->next;
+ free(cp);
+ }
+ free(vp);
+ return 0;
+}
+
+void*
+_vm_resize(register Vmalloc_t* vp, void* o, unsigned long size)
+{
+ char* p;
+ unsigned long n;
+ unsigned long z;
+
+ z = vp->last;
+ vp->last = size;
+ if (o && size < z)
+ return o;
+ if ((o ? (size - z) : size) > vp->size)
+ {
+ n = (size > sizeof(vp->current->data)) ? (size - sizeof(vp->current->data)) : 0;
+ if (!(vp->current->next = newof(0, Vmchunk_t, 1, n)))
+ return 0;
+ vp->current = vp->current->next;
+ vp->data = vp->current->data;
+ vp->size = n ? 0 : sizeof(vp->current->data);
+ if (o)
+ {
+ memcpy(vp->data, o, z);
+ o = (void*)vp->data;
+ }
+ }
+ else if (o)
+ size -= z;
+ p = vp->data;
+ size = roundof(size, VM_ALIGN);
+ if (size >= vp->size)
+ vp->size = 0;
+ else
+ {
+ vp->size -= size;
+ vp->data += size;
+ }
+ return p;
+}