move example code to .h file
This commit is contained in:
parent
b332ecf06d
commit
c128b97ad5
63
test/plex.c
63
test/plex.c
|
|
@ -1,66 +1,5 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct arena_s Arena;
|
||||
struct arena_s {
|
||||
unsigned char *tape;
|
||||
unsigned int count;
|
||||
unsigned int capacity;
|
||||
};
|
||||
|
||||
void *aalloc(Arena *arena, unsigned int size){
|
||||
unsigned int pos;
|
||||
if(arena == NULL) return NULL;
|
||||
if(arena->count + size > arena->capacity) return NULL;
|
||||
|
||||
pos = arena->count;
|
||||
arena->count += size;
|
||||
return (void *)&arena->tape[pos];
|
||||
}
|
||||
|
||||
unsigned int
|
||||
scpy(char *to, const char *from, unsigned int length)
|
||||
{
|
||||
unsigned int i;
|
||||
if(to == NULL || from == NULL) return -1;
|
||||
if(length == 0) return 0;
|
||||
for(i = 0; i < length - 1 && from[i] != '\0'; i++) to[i] = from[i];
|
||||
to[i] = '\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *
|
||||
ascpy(Arena *arena, const char *start, unsigned int length)
|
||||
{
|
||||
char *str;
|
||||
if(!start) return NULL;
|
||||
str = (char *)aalloc(arena, length + 1);
|
||||
if(!str) return NULL;
|
||||
scpy(str, start, length);
|
||||
return str;
|
||||
}
|
||||
|
||||
void *
|
||||
areturn(Arena *arena, unsigned int checkpoint, const void *src, unsigned int size)
|
||||
{
|
||||
void *dest;
|
||||
if(arena == NULL || src == NULL) return NULL;
|
||||
|
||||
dest = (void *)&arena->tape[checkpoint];
|
||||
if (src == dest) return dest;
|
||||
|
||||
memmove(dest, src, size);
|
||||
arena->count = checkpoint + size;
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
#define ARENA_RETURN(arena, ckpt, src_ptr, type) \
|
||||
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type))
|
||||
|
||||
#define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \
|
||||
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type) * (count))
|
||||
#include "undar.h"
|
||||
|
||||
typedef struct Point Point;
|
||||
struct Point {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef UNDAR_H
|
||||
#define UNDAR_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct arena_s Arena;
|
||||
struct arena_s {
|
||||
unsigned char *tape;
|
||||
unsigned int count;
|
||||
unsigned int capacity;
|
||||
};
|
||||
|
||||
static void *aalloc(Arena *arena, unsigned int size){
|
||||
unsigned int pos;
|
||||
if(arena == NULL) return NULL;
|
||||
if(arena->count + size > arena->capacity) return NULL;
|
||||
|
||||
pos = arena->count;
|
||||
arena->count += size;
|
||||
return (void *)&arena->tape[pos];
|
||||
}
|
||||
|
||||
static void *
|
||||
areturn(Arena *arena, unsigned int checkpoint, const void *src, unsigned int size)
|
||||
{
|
||||
void *dest;
|
||||
if(arena == NULL || src == NULL) return NULL;
|
||||
|
||||
dest = (void *)&arena->tape[checkpoint];
|
||||
if (src == dest) return dest;
|
||||
|
||||
memmove(dest, src, size);
|
||||
arena->count = checkpoint + size;
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
#define ARENA_RETURN(arena, ckpt, src_ptr, type) \
|
||||
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type))
|
||||
|
||||
#define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \
|
||||
return (type *)areturn((arena), (ckpt), (src_ptr), sizeof(type) * (count))
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue