Added the ADT library (not really abstract, but it's a common data type library)
authorJohn Hodge <[email protected]>
Thu, 15 Apr 2010 02:09:47 +0000 (10:09 +0800)
committerJohn Hodge <[email protected]>
Thu, 15 Apr 2010 02:09:47 +0000 (10:09 +0800)
- Currenly only includes Ring Buffer code

Kernel/adt.c [new file with mode: 0644]
Kernel/include/adt.h [new file with mode: 0644]

diff --git a/Kernel/adt.c b/Kernel/adt.c
new file mode 100644 (file)
index 0000000..7a23834
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ */
+#include <acess.h>
+#include <adt.h>
+
+// === CODE ===
+// --- Ring Buffers ---
+tRingBuffer *RingBuffer_Create(size_t Space)
+{
+       tRingBuffer     *ret = malloc(sizeof(tRingBuffer)+Space);
+       ret->Start = 0;
+       ret->Length = 0;
+       ret->Space = Space;
+       return ret;
+}
+
+size_t RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length)
+{
+       if(Length > Buffer->Length)     Length = Buffer->Length;
+       
+       if( Buffer->Start + Length > Buffer->Space )
+       {
+                int    endData = Buffer->Space - Buffer->Start;
+               memcpy(Dest, &Buffer->Data[Buffer->Start], endData);
+               memcpy(Dest + endData, &Buffer->Data, Length - endData);
+       }
+       else
+       {
+               memcpy(Dest, &Buffer->Data[Buffer->Start], Length);
+       }
+       Buffer->Length -= Length;
+       return Length;
+}
+
+size_t RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length)
+{
+       size_t  bufEnd = Buffer->Start + Buffer->Length;
+       size_t  endSpace = Buffer->Space - bufEnd;
+       
+       // Force to bounds
+       if(Length > Buffer->Space - Buffer->Length)
+               Length = Buffer->Space - Buffer->Length;
+       
+       if(endSpace < Length)
+       {
+               memcpy( &Buffer->Data[bufEnd], Source, endSpace );
+               memcpy( Buffer->Data, Source + endSpace, Length - endSpace );
+               Buffer->Length = Length - endSpace;
+       }
+       else
+       {
+               memcpy( &Buffer->Data[bufEnd], Source, Length );
+               Buffer->Length += Length;
+       }
+       
+       return Length;
+}
diff --git a/Kernel/include/adt.h b/Kernel/include/adt.h
new file mode 100644 (file)
index 0000000..724877c
--- /dev/null
@@ -0,0 +1,33 @@
+/* 
+ * Acess2
+ * - Abstract Data Types
+ */
+#ifndef _ADT_H_
+#define _ADT_H_
+
+/**
+ * \name Ring Buffer
+ * \{
+ */
+typedef struct sRingBuffer
+{
+       size_t  Start;
+       size_t  Length;
+       size_t  Space;
+       char    Data[];
+}      tRingBuffer;
+
+/**
+ * \brief Create a ring buffer \a Space bytes large
+ * \param Space        Ammount of space to allocate within the buffer
+ * \return Pointer to the buffer structure
+ */
+extern tRingBuffer     *RingBuffer_Create(size_t Space);
+extern size_t  RingBuffer_Read(void *Dest, tRingBuffer *Buffer, size_t Length);
+extern size_t  RingBuffer_Write(tRingBuffer *Buffer, void *Source, size_t Length);
+/**
+ * \}
+ */
+
+
+#endif

UCC git Repository :: git.ucc.asn.au