[Frugalware-git] firepl: initial draft of dpa module

James Buren ryuo at frugalware.org
Tue May 8 12:51:35 CEST 2012


Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=firepl.git;a=commitdiff;h=6447c942f10f35e084a0fbea92ca55a0e7752461

commit 6447c942f10f35e084a0fbea92ca55a0e7752461
Author: James Buren <ryuo at frugalware.org>
Date:   Tue May 8 05:51:24 2012 -0500

initial draft of dpa module

diff --git a/dpa.c b/dpa.c
new file mode 100644
index 0000000..fad69c0
--- /dev/null
+++ b/dpa.c
@@ -0,0 +1,73 @@
+#include <stdlib.h>
+#include "utility.h"
+#include "dpa.h"
+
+#define DPA_DEFAULT_SIZE 64
+
+struct _dpa
+{
+	void **ptr;
+	size_t len;
+	size_t size;
+};
+
+extern dpa *dpa_open(void)
+{
+	dpa *self;
+
+	self = alloc(dpa,1);
+
+	self->ptr = alloc(void *,DPA_DEFAULT_SIZE);
+
+	self->len = 0;
+
+	self->size = DPA_DEFAULT_SIZE;
+
+	return self;
+}
+
+extern void dpa_append(dpa *self,void *p)
+{
+	assert(self);
+
+	if(self->len == self->size)
+	{
+		self->size *= 2;
+
+		self->ptr = redim(self->ptr,void *,self->size);
+	}
+
+	self->ptr[self->len] = p;
+
+	if(p)
+		++self->len;
+}
+
+extern void **dpa_fetch(const dpa *self)
+{
+	assert(self);
+
+	return self->ptr;
+}
+
+extern void **dpa_close(dpa *self,void (*func) (void *p))
+{
+	size_t n;
+	void **p;
+
+	assert(self);
+
+	if(func)
+	{
+		for( n = 0 ; n < self->len ; ++n )
+			func(self->ptr[n]);
+
+		p = 0;
+	}
+	else
+		p = redim(self->ptr,void *,self->len + 1);
+
+	free(self);
+
+	return p;
+}
diff --git a/dpa.h b/dpa.h
new file mode 100644
index 0000000..f46305c
--- /dev/null
+++ b/dpa.h
@@ -0,0 +1,8 @@
+#pragma once
+
+typedef struct _dpa dpa;
+
+extern dpa *dpa_open(void);
+extern void dpa_append(dpa *self,void *p);
+extern void **dpa_fetch(const dpa *self);
+extern void **dpa_close(dpa *self,void (*func) (void *p));


More information about the Frugalware-git mailing list