1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/param.h>
struct simplebuffer_s {
uint8_t *buffer;
int atomsize;
int atoms;
int fill;
int headroom;
};
void *sb_init(int atoms, int atomsize, int headroom) {
struct simplebuffer_s *sb;
sb=calloc(1, sizeof(struct simplebuffer_s));
if (!sb)
return NULL;
sb->buffer=malloc(atoms*atomsize+headroom);
if (!sb->buffer) {
free(sb);
return NULL;
}
sb->atoms=atoms;
sb->atomsize=atomsize;
sb->headroom=headroom;
return sb;
}
void sb_free(void *sbv) {
struct simplebuffer_s *sb=sbv;
free(sb->buffer);
free(sb);
}
int sb_used_atoms(void *sbv) {
struct simplebuffer_s *sb=sbv;
return sb->fill;
}
int sb_free_atoms(void *sbv) {
struct simplebuffer_s *sb=sbv;
return (sb->atoms-sb->fill);
}
int sb_add_atoms(void *sbv, uint8_t *atom, int atoms) {
struct simplebuffer_s *sb=sbv;
int copy;
copy=MIN(atoms, sb_free_atoms(sbv));
memcpy(&sb->buffer[sb->fill*sb->atomsize+sb->headroom], atom, copy*sb->atomsize);
sb->fill+=copy;
return copy;
}
uint8_t *sb_bufptr(void *sbv) {
struct simplebuffer_s *sb=sbv;
return sb->buffer;
}
int sb_buflen(void *sbv) {
struct simplebuffer_s *sb=sbv;
return sb->fill*sb->atomsize+sb->headroom;
}
void sb_zap(void *sbv) {
struct simplebuffer_s *sb=sbv;
sb->fill=0;
}
|