add nestegg and halloc from http://github.com/kinetiknz/nestegg.git
This commit is contained in:
parent
0c7bdbcf54
commit
6b297cd037
9 changed files with 2790 additions and 0 deletions
14
src/halloc/Makefile
Normal file
14
src/halloc/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
CFLAGS = -I. -ansi -Wall -pedantic
|
||||||
|
|
||||||
|
LIBNAME = libhalloc.a
|
||||||
|
OBJS = src/halloc.o
|
||||||
|
|
||||||
|
$(LIBNAME): $(OBJS)
|
||||||
|
ar rcs $(LIBNAME) $(OBJS)
|
||||||
|
|
||||||
|
install: $(LIBNAME)
|
||||||
|
cp halloc.h /usr/include
|
||||||
|
cp $(LIBNAME) /usr/lib
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(LIBNAME) $(OBJS)
|
45
src/halloc/README
Normal file
45
src/halloc/README
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
halloc 1.2.1
|
||||||
|
============
|
||||||
|
|
||||||
|
Hierarchical memory heap interface - an extension to standard
|
||||||
|
malloc/free interface that simplifies tasks of memory disposal
|
||||||
|
when allocated structures exhibit hierarchical properties.
|
||||||
|
|
||||||
|
http://swapped.cc/halloc
|
||||||
|
=
|
||||||
|
To build libhalloc.a with GNU tools run
|
||||||
|
make
|
||||||
|
|
||||||
|
To install in /usr/include and /usr/lib
|
||||||
|
make install
|
||||||
|
|
||||||
|
To cleanup the build files
|
||||||
|
make clean
|
||||||
|
=
|
||||||
|
halloc-1.2.1
|
||||||
|
* fixed a double-free bug in _set_allocator() as per
|
||||||
|
Matthew Gregan comments
|
||||||
|
|
||||||
|
* switched to using NULL instead of 0 where applicable
|
||||||
|
|
||||||
|
halloc-1.2.0
|
||||||
|
* added missing <string.h> include to halloc.c
|
||||||
|
|
||||||
|
* improved standard compliance thanks to the feedback
|
||||||
|
received from Stan Tobias. Two things were fixed -
|
||||||
|
|
||||||
|
- hblock_t structure no longer uses zero-sized 'data'
|
||||||
|
array, which happened to be common, but non-standard
|
||||||
|
extension;
|
||||||
|
|
||||||
|
- secondly, added the code to test the behaviour of
|
||||||
|
realloc(ptr, 0). Standard allows it NOT to act as
|
||||||
|
free(), in which case halloc will use its own version
|
||||||
|
of allocator calling free() when neccessary.
|
||||||
|
|
||||||
|
halloc-1.1.0
|
||||||
|
* initial public release (rewrite of hhmalloc library)
|
||||||
|
|
||||||
|
=============================================================================
|
||||||
|
Copyright (c) 2004-2010, Alex Pankratov (ap@swapped.cc). All rights reserved.
|
||||||
|
|
43
src/halloc/halloc.h
Normal file
43
src/halloc/halloc.h
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 Alex Pankratov. All rights reserved.
|
||||||
|
*
|
||||||
|
* Hierarchical memory allocator, 1.2.1
|
||||||
|
* http://swapped.cc/halloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The program is distributed under terms of BSD license.
|
||||||
|
* You can obtain the copy of the license by visiting:
|
||||||
|
*
|
||||||
|
* http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBP_HALLOC_H_
|
||||||
|
#define _LIBP_HALLOC_H_
|
||||||
|
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Core API
|
||||||
|
*/
|
||||||
|
void * halloc (void * block, size_t len);
|
||||||
|
void hattach(void * block, void * parent);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* standard malloc/free api
|
||||||
|
*/
|
||||||
|
void * h_malloc (size_t len);
|
||||||
|
void * h_calloc (size_t n, size_t len);
|
||||||
|
void * h_realloc(void * p, size_t len);
|
||||||
|
void h_free (void * p);
|
||||||
|
char * h_strdup (const char * str);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* the underlying allocator
|
||||||
|
*/
|
||||||
|
typedef void * (* realloc_t)(void * ptr, size_t len);
|
||||||
|
|
||||||
|
extern realloc_t halloc_allocator;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
36
src/halloc/src/align.h
Normal file
36
src/halloc/src/align.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 Alex Pankratov. All rights reserved.
|
||||||
|
*
|
||||||
|
* Hierarchical memory allocator, 1.2.1
|
||||||
|
* http://swapped.cc/halloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The program is distributed under terms of BSD license.
|
||||||
|
* You can obtain the copy of the license by visiting:
|
||||||
|
*
|
||||||
|
* http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBP_ALIGN_H_
|
||||||
|
#define _LIBP_ALIGN_H_
|
||||||
|
|
||||||
|
/*
|
||||||
|
* a type with the most strict alignment requirements
|
||||||
|
*/
|
||||||
|
union max_align
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
short s;
|
||||||
|
long l;
|
||||||
|
int i;
|
||||||
|
float f;
|
||||||
|
double d;
|
||||||
|
void * v;
|
||||||
|
void (*q)(void);
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef union max_align max_align_t;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
254
src/halloc/src/halloc.c
Normal file
254
src/halloc/src/halloc.c
Normal file
|
@ -0,0 +1,254 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004i-2010 Alex Pankratov. All rights reserved.
|
||||||
|
*
|
||||||
|
* Hierarchical memory allocator, 1.2.1
|
||||||
|
* http://swapped.cc/halloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The program is distributed under terms of BSD license.
|
||||||
|
* You can obtain the copy of the license by visiting:
|
||||||
|
*
|
||||||
|
* http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h> /* realloc */
|
||||||
|
#include <string.h> /* memset & co */
|
||||||
|
|
||||||
|
#include "halloc.h"
|
||||||
|
#include "align.h"
|
||||||
|
#include "hlist.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* block control header
|
||||||
|
*/
|
||||||
|
typedef struct hblock
|
||||||
|
{
|
||||||
|
#ifndef NDEBUG
|
||||||
|
#define HH_MAGIC 0x20040518L
|
||||||
|
long magic;
|
||||||
|
#endif
|
||||||
|
hlist_item_t siblings; /* 2 pointers */
|
||||||
|
hlist_head_t children; /* 1 pointer */
|
||||||
|
max_align_t data[1]; /* not allocated, see below */
|
||||||
|
|
||||||
|
} hblock_t;
|
||||||
|
|
||||||
|
#define sizeof_hblock offsetof(hblock_t, data)
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
realloc_t halloc_allocator = NULL;
|
||||||
|
|
||||||
|
#define allocator halloc_allocator
|
||||||
|
|
||||||
|
/*
|
||||||
|
* static methods
|
||||||
|
*/
|
||||||
|
static void _set_allocator(void);
|
||||||
|
static void * _realloc(void * ptr, size_t n);
|
||||||
|
|
||||||
|
static int _relate(hblock_t * b, hblock_t * p);
|
||||||
|
static void _free_children(hblock_t * p);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Core API
|
||||||
|
*/
|
||||||
|
void * halloc(void * ptr, size_t len)
|
||||||
|
{
|
||||||
|
hblock_t * p;
|
||||||
|
|
||||||
|
/* set up default allocator */
|
||||||
|
if (! allocator)
|
||||||
|
{
|
||||||
|
_set_allocator();
|
||||||
|
assert(allocator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* calloc */
|
||||||
|
if (! ptr)
|
||||||
|
{
|
||||||
|
if (! len)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
p = allocator(0, len + sizeof_hblock);
|
||||||
|
if (! p)
|
||||||
|
return NULL;
|
||||||
|
#ifndef NDEBUG
|
||||||
|
p->magic = HH_MAGIC;
|
||||||
|
#endif
|
||||||
|
hlist_init(&p->children);
|
||||||
|
hlist_init_item(&p->siblings);
|
||||||
|
|
||||||
|
return p->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = structof(ptr, hblock_t, data);
|
||||||
|
assert(p->magic == HH_MAGIC);
|
||||||
|
|
||||||
|
/* realloc */
|
||||||
|
if (len)
|
||||||
|
{
|
||||||
|
p = allocator(p, len + sizeof_hblock);
|
||||||
|
if (! p)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
hlist_relink(&p->siblings);
|
||||||
|
hlist_relink_head(&p->children);
|
||||||
|
|
||||||
|
return p->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* free */
|
||||||
|
_free_children(p);
|
||||||
|
hlist_del(&p->siblings);
|
||||||
|
allocator(p, 0);
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hattach(void * block, void * parent)
|
||||||
|
{
|
||||||
|
hblock_t * b, * p;
|
||||||
|
|
||||||
|
if (! block)
|
||||||
|
{
|
||||||
|
assert(! parent);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* detach */
|
||||||
|
b = structof(block, hblock_t, data);
|
||||||
|
assert(b->magic == HH_MAGIC);
|
||||||
|
|
||||||
|
hlist_del(&b->siblings);
|
||||||
|
|
||||||
|
if (! parent)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* attach */
|
||||||
|
p = structof(parent, hblock_t, data);
|
||||||
|
assert(p->magic == HH_MAGIC);
|
||||||
|
|
||||||
|
/* sanity checks */
|
||||||
|
assert(b != p); /* trivial */
|
||||||
|
assert(! _relate(p, b)); /* heavy ! */
|
||||||
|
|
||||||
|
hlist_add(&p->children, &b->siblings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* malloc/free api
|
||||||
|
*/
|
||||||
|
void * h_malloc(size_t len)
|
||||||
|
{
|
||||||
|
return halloc(0, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void * h_calloc(size_t n, size_t len)
|
||||||
|
{
|
||||||
|
void * ptr = halloc(0, len*=n);
|
||||||
|
return ptr ? memset(ptr, 0, len) : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void * h_realloc(void * ptr, size_t len)
|
||||||
|
{
|
||||||
|
return halloc(ptr, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void h_free(void * ptr)
|
||||||
|
{
|
||||||
|
halloc(ptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char * h_strdup(const char * str)
|
||||||
|
{
|
||||||
|
size_t len = strlen(str);
|
||||||
|
char * ptr = halloc(0, len + 1);
|
||||||
|
return ptr ? (ptr[len] = 0, memcpy(ptr, str, len)) : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* static stuff
|
||||||
|
*/
|
||||||
|
static void _set_allocator(void)
|
||||||
|
{
|
||||||
|
void * p;
|
||||||
|
assert(! allocator);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* the purpose of the test below is to check the behaviour
|
||||||
|
* of realloc(ptr, 0), which is defined in the standard
|
||||||
|
* as an implementation-specific. if it returns zero,
|
||||||
|
* then it's equivalent to free(). it can however return
|
||||||
|
* non-zero, in which case it cannot be used for freeing
|
||||||
|
* memory blocks and we'll need to supply our own version
|
||||||
|
*
|
||||||
|
* Thanks to Stan Tobias for pointing this tricky part out.
|
||||||
|
*/
|
||||||
|
allocator = realloc;
|
||||||
|
if (! (p = malloc(1)))
|
||||||
|
/* hmm */
|
||||||
|
return;
|
||||||
|
|
||||||
|
if ((p = realloc(p, 0)))
|
||||||
|
{
|
||||||
|
/* realloc cannot be used as free() */
|
||||||
|
allocator = _realloc;
|
||||||
|
free(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void * _realloc(void * ptr, size_t n)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* free'ing realloc()
|
||||||
|
*/
|
||||||
|
if (n)
|
||||||
|
return realloc(ptr, n);
|
||||||
|
free(ptr);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int _relate(hblock_t * b, hblock_t * p)
|
||||||
|
{
|
||||||
|
hlist_item_t * i;
|
||||||
|
|
||||||
|
if (!b || !p)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* since there is no 'parent' pointer, which would've allowed
|
||||||
|
* O(log(n)) upward traversal, the check must use O(n) downward
|
||||||
|
* iteration of the entire hierarchy; and this can be VERY SLOW
|
||||||
|
*/
|
||||||
|
hlist_for_each(i, &p->children)
|
||||||
|
{
|
||||||
|
hblock_t * q = structof(i, hblock_t, siblings);
|
||||||
|
if (q == b || _relate(b, q))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _free_children(hblock_t * p)
|
||||||
|
{
|
||||||
|
hlist_item_t * i, * tmp;
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
/*
|
||||||
|
* this catches loops in hierarchy with almost zero
|
||||||
|
* overhead (compared to _relate() running time)
|
||||||
|
*/
|
||||||
|
assert(p && p->magic == HH_MAGIC);
|
||||||
|
p->magic = 0;
|
||||||
|
#endif
|
||||||
|
hlist_for_each_safe(i, tmp, &p->children)
|
||||||
|
{
|
||||||
|
hblock_t * q = structof(i, hblock_t, siblings);
|
||||||
|
_free_children(q);
|
||||||
|
allocator(q, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
136
src/halloc/src/hlist.h
Normal file
136
src/halloc/src/hlist.h
Normal file
|
@ -0,0 +1,136 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 Alex Pankratov. All rights reserved.
|
||||||
|
*
|
||||||
|
* Hierarchical memory allocator, 1.2.1
|
||||||
|
* http://swapped.cc/halloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The program is distributed under terms of BSD license.
|
||||||
|
* You can obtain the copy of the license by visiting:
|
||||||
|
*
|
||||||
|
* http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBP_HLIST_H_
|
||||||
|
#define _LIBP_HLIST_H_
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include "macros.h" /* static_inline */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* weak double-linked list w/ tail sentinel
|
||||||
|
*/
|
||||||
|
typedef struct hlist_head hlist_head_t;
|
||||||
|
typedef struct hlist_item hlist_item_t;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
struct hlist_head
|
||||||
|
{
|
||||||
|
hlist_item_t * next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct hlist_item
|
||||||
|
{
|
||||||
|
hlist_item_t * next;
|
||||||
|
hlist_item_t ** prev;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* shared tail sentinel
|
||||||
|
*/
|
||||||
|
struct hlist_item hlist_null;
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#define __hlist_init(h) { &hlist_null }
|
||||||
|
#define __hlist_init_item(i) { &hlist_null, &(i).next }
|
||||||
|
|
||||||
|
static_inline void hlist_init(hlist_head_t * h);
|
||||||
|
static_inline void hlist_init_item(hlist_item_t * i);
|
||||||
|
|
||||||
|
/* static_inline void hlist_purge(hlist_head_t * h); */
|
||||||
|
|
||||||
|
/* static_inline bool_t hlist_empty(const hlist_head_t * h); */
|
||||||
|
|
||||||
|
/* static_inline hlist_item_t * hlist_head(const hlist_head_t * h); */
|
||||||
|
|
||||||
|
/* static_inline hlist_item_t * hlist_next(const hlist_item_t * i); */
|
||||||
|
/* static_inline hlist_item_t * hlist_prev(const hlist_item_t * i,
|
||||||
|
const hlist_head_t * h); */
|
||||||
|
|
||||||
|
static_inline void hlist_add(hlist_head_t * h, hlist_item_t * i);
|
||||||
|
|
||||||
|
/* static_inline void hlist_add_prev(hlist_item_t * l, hlist_item_t * i); */
|
||||||
|
/* static_inline void hlist_add_next(hlist_item_t * l, hlist_item_t * i); */
|
||||||
|
|
||||||
|
static_inline void hlist_del(hlist_item_t * i);
|
||||||
|
|
||||||
|
static_inline void hlist_relink(hlist_item_t * i);
|
||||||
|
static_inline void hlist_relink_head(hlist_head_t * h);
|
||||||
|
|
||||||
|
#define hlist_for_each(i, h) \
|
||||||
|
for (i = (h)->next; i != &hlist_null; i = i->next)
|
||||||
|
|
||||||
|
#define hlist_for_each_safe(i, tmp, h) \
|
||||||
|
for (i = (h)->next, tmp = i->next; \
|
||||||
|
i!= &hlist_null; \
|
||||||
|
i = tmp, tmp = i->next)
|
||||||
|
|
||||||
|
/*
|
||||||
|
* static
|
||||||
|
*/
|
||||||
|
static_inline void hlist_init(hlist_head_t * h)
|
||||||
|
{
|
||||||
|
assert(h);
|
||||||
|
h->next = &hlist_null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_inline void hlist_init_item(hlist_item_t * i)
|
||||||
|
{
|
||||||
|
assert(i);
|
||||||
|
i->prev = &i->next;
|
||||||
|
i->next = &hlist_null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_inline void hlist_add(hlist_head_t * h, hlist_item_t * i)
|
||||||
|
{
|
||||||
|
hlist_item_t * next;
|
||||||
|
assert(h && i);
|
||||||
|
|
||||||
|
next = i->next = h->next;
|
||||||
|
next->prev = &i->next;
|
||||||
|
h->next = i;
|
||||||
|
i->prev = &h->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_inline void hlist_del(hlist_item_t * i)
|
||||||
|
{
|
||||||
|
hlist_item_t * next;
|
||||||
|
assert(i);
|
||||||
|
|
||||||
|
next = i->next;
|
||||||
|
next->prev = i->prev;
|
||||||
|
*i->prev = next;
|
||||||
|
|
||||||
|
hlist_init_item(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
static_inline void hlist_relink(hlist_item_t * i)
|
||||||
|
{
|
||||||
|
assert(i);
|
||||||
|
*i->prev = i;
|
||||||
|
i->next->prev = &i->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
static_inline void hlist_relink_head(hlist_head_t * h)
|
||||||
|
{
|
||||||
|
assert(h);
|
||||||
|
h->next->prev = &h->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
36
src/halloc/src/macros.h
Normal file
36
src/halloc/src/macros.h
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2004-2010 Alex Pankratov. All rights reserved.
|
||||||
|
*
|
||||||
|
* Hierarchical memory allocator, 1.2.1
|
||||||
|
* http://swapped.cc/halloc
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The program is distributed under terms of BSD license.
|
||||||
|
* You can obtain the copy of the license by visiting:
|
||||||
|
*
|
||||||
|
* http://www.opensource.org/licenses/bsd-license.php
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _LIBP_MACROS_H_
|
||||||
|
#define _LIBP_MACROS_H_
|
||||||
|
|
||||||
|
#include <stddef.h> /* offsetof */
|
||||||
|
|
||||||
|
/*
|
||||||
|
restore pointer to the structure by a pointer to its field
|
||||||
|
*/
|
||||||
|
#define structof(p,t,f) ((t*)(- offsetof(t,f) + (char*)(p)))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* redefine for the target compiler
|
||||||
|
*/
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define static_inline static __inline
|
||||||
|
#else
|
||||||
|
#define static_inline static __inline__
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
1938
src/nestegg.c
Normal file
1938
src/nestegg.c
Normal file
File diff suppressed because it is too large
Load diff
288
src/nestegg.h
Normal file
288
src/nestegg.h
Normal file
|
@ -0,0 +1,288 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2010 Matthew Gregan <kinetik@flim.org>
|
||||||
|
*
|
||||||
|
* This program is made available under an ISC-style license. See the
|
||||||
|
* accompanying file LICENSE for details.
|
||||||
|
*/
|
||||||
|
#ifndef NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79
|
||||||
|
#define NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
typedef __int64 int64_t;
|
||||||
|
typedef unsigned __int64 uint64_t;
|
||||||
|
#else
|
||||||
|
#include <stdint.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** @mainpage
|
||||||
|
|
||||||
|
@section intro Introduction
|
||||||
|
|
||||||
|
This is the documentation fot the <tt>libnestegg</tt> C API.
|
||||||
|
<tt>libnestegg</tt> is a demultiplexing library for <a
|
||||||
|
href="http://www.matroska.org/">Matroska</a> and <a
|
||||||
|
href="http://www.webmproject.org/">WebMedia</a> media files.
|
||||||
|
|
||||||
|
@section example Example code
|
||||||
|
|
||||||
|
@code
|
||||||
|
nestegg * demux_ctx;
|
||||||
|
nestegg_init(&demux_ctx, io, NULL);
|
||||||
|
|
||||||
|
nestegg_packet * pkt;
|
||||||
|
while ((r = nestegg_read_packet(demux_ctx, &pkt)) > 0) {
|
||||||
|
unsigned int track;
|
||||||
|
|
||||||
|
nestegg_packet_track(pkt, &track);
|
||||||
|
|
||||||
|
// This example decodes the first track only.
|
||||||
|
if (track == 0) {
|
||||||
|
unsigned int chunk, chunks;
|
||||||
|
|
||||||
|
nestegg_packet_count(pkt, &chunks);
|
||||||
|
|
||||||
|
// Decode each chunk of data.
|
||||||
|
for (chunk = 0; chunk < chunks; ++chunk) {
|
||||||
|
unsigned char * data;
|
||||||
|
size_t data_size;
|
||||||
|
|
||||||
|
nestegg_packet_data(pkt, chunk, &data, &data_size);
|
||||||
|
|
||||||
|
example_codec_decode(codec_ctx, data, data_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nestegg_free_packet(pkt);
|
||||||
|
}
|
||||||
|
|
||||||
|
nestegg_destroy(demux_ctx);
|
||||||
|
@endcode
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/** @file
|
||||||
|
The <tt>libnestegg</tt> C API. */
|
||||||
|
|
||||||
|
#define NESTEGG_TRACK_VIDEO 0 /**< Track is of type video. */
|
||||||
|
#define NESTEGG_TRACK_AUDIO 1 /**< Track is of type audio. */
|
||||||
|
|
||||||
|
#define NESTEGG_CODEC_VP8 0 /**< Track uses Google On2 VP8 codec. */
|
||||||
|
#define NESTEGG_CODEC_VORBIS 1 /**< Track uses Xiph Vorbis codec. */
|
||||||
|
|
||||||
|
#define NESTEGG_SEEK_SET 0 /**< Seek offset relative to beginning of stream. */
|
||||||
|
#define NESTEGG_SEEK_CUR 1 /**< Seek offset relative to current position in stream. */
|
||||||
|
#define NESTEGG_SEEK_END 2 /**< Seek offset relative to end of stream. */
|
||||||
|
|
||||||
|
#define NESTEGG_LOG_DEBUG 1 /**< Debug level log message. */
|
||||||
|
#define NESTEGG_LOG_INFO 10 /**< Informational level log message. */
|
||||||
|
#define NESTEGG_LOG_WARNING 100 /**< Warning level log message. */
|
||||||
|
#define NESTEGG_LOG_ERROR 1000 /**< Error level log message. */
|
||||||
|
#define NESTEGG_LOG_CRITICAL 10000 /**< Critical level log message. */
|
||||||
|
|
||||||
|
typedef struct nestegg nestegg; /**< Opaque handle referencing the stream state. */
|
||||||
|
typedef struct nestegg_packet nestegg_packet; /**< Opaque handle referencing a packet of data. */
|
||||||
|
|
||||||
|
/** User supplied IO context. */
|
||||||
|
typedef struct {
|
||||||
|
/** User supplied read callback.
|
||||||
|
@param buffer Buffer to read data into.
|
||||||
|
@param length Length of supplied buffer in bytes.
|
||||||
|
@param userptr The #userdata supplied by the user.
|
||||||
|
@retval 1 Read succeeded.
|
||||||
|
@retval 0 End of stream.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int (* read)(void * buffer, size_t length, void * userdata);
|
||||||
|
|
||||||
|
/** User supplied seek callback.
|
||||||
|
@param offset Offset within the stream to seek to.
|
||||||
|
@param whence Seek direction. One of #NESTEGG_SEEK_SET,
|
||||||
|
#NESTEGG_SEEK_CUR, or #NESTEGG_SEEK_END.
|
||||||
|
@param userdata The #userdata supplied by the user.
|
||||||
|
@retval 0 Seek succeeded.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int (* seek)(int64_t offset, int whence, void * userdata);
|
||||||
|
|
||||||
|
/** User supplied tell callback.
|
||||||
|
@param userdata The #userdata supplied by the user.
|
||||||
|
@returns Current position within the stream.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int64_t (* tell)(void * userdata);
|
||||||
|
|
||||||
|
/** User supplied pointer to be passed to the IO callbacks. */
|
||||||
|
void * userdata;
|
||||||
|
} nestegg_io;
|
||||||
|
|
||||||
|
/** Parameters specific to a video track. */
|
||||||
|
typedef struct {
|
||||||
|
unsigned int width; /**< Width of the video frame in pixels. */
|
||||||
|
unsigned int height; /**< Height of the video frame in pixels. */
|
||||||
|
unsigned int display_width; /**< Display width of the video frame in pixels. */
|
||||||
|
unsigned int display_height; /**< Display height of the video frame in pixels. */
|
||||||
|
unsigned int crop_bottom; /**< Pixels to crop from the bottom of the frame. */
|
||||||
|
unsigned int crop_top; /**< Pixels to crop from the top of the frame. */
|
||||||
|
unsigned int crop_left; /**< Pixels to crop from the left of the frame. */
|
||||||
|
unsigned int crop_right; /**< Pixels to crop from the right of the frame. */
|
||||||
|
} nestegg_video_params;
|
||||||
|
|
||||||
|
/** Parameters specific to an audio track. */
|
||||||
|
typedef struct {
|
||||||
|
double rate; /**< Sampling rate in Hz. */
|
||||||
|
unsigned int channels; /**< Number of audio channels. */
|
||||||
|
unsigned int depth; /**< Bits per sample. */
|
||||||
|
} nestegg_audio_params;
|
||||||
|
|
||||||
|
/** Logging callback function pointer. */
|
||||||
|
typedef void (* nestegg_log)(nestegg * context, unsigned int severity, char const * format, ...);
|
||||||
|
|
||||||
|
/** Initialize a nestegg context. During initialization the parser will
|
||||||
|
read forward in the stream processing all elements until the first
|
||||||
|
block of media is reached. All track metadata has been processed at this point.
|
||||||
|
@param context Storage for the new nestegg context. @see nestegg_destroy
|
||||||
|
@param io User supplied IO context.
|
||||||
|
@param callback Optional logging callback function pointer. May be NULL.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_init(nestegg ** context, nestegg_io io, nestegg_log callback);
|
||||||
|
|
||||||
|
/** Destroy a nestegg context and free associated memory.
|
||||||
|
@param context #nestegg context to be freed. @see nestegg_init */
|
||||||
|
void nestegg_destroy(nestegg * context);
|
||||||
|
|
||||||
|
/** Query the duration of the media stream in nanoseconds.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param duration Storage for the queried duration.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_duration(nestegg * context, uint64_t * duration);
|
||||||
|
|
||||||
|
/** Query the number of tracks in the media stream.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param tracks Storage for the queried track count.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_count(nestegg * context, unsigned int * tracks);
|
||||||
|
|
||||||
|
/** Seek @a track to @a tstamp. Stream seek will terminate at the earliest
|
||||||
|
key point in the stream at or before @a tstamp. Other tracks in the
|
||||||
|
stream will output packets with unspecified but nearby timestamps.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@param tstamp Absolute timestamp in nanoseconds.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_seek(nestegg * context, unsigned int track, uint64_t tstamp);
|
||||||
|
|
||||||
|
/** Query the type specified by @a track.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@retval #NESTEGG_TRACK_VIDEO Track type is video.
|
||||||
|
@retval #NESTEGG_TRACK_VIDEO Track type is audio.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_type(nestegg * context, unsigned int track);
|
||||||
|
|
||||||
|
/** Query the codec ID specified by @a track.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@retval #NESTEGG_CODEC_VP8 Track codec is VP8.
|
||||||
|
@retval #NESTEGG_CODEC_VORBIS Track codec is Vorbis.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_codec_id(nestegg * context, unsigned int track);
|
||||||
|
|
||||||
|
/** Query the number of codec initialization chunks for @a track. Each
|
||||||
|
chunk of data should be passed to the codec initialization functions in
|
||||||
|
the order returned.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@param count Storage for the queried chunk count.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_codec_data_count(nestegg * context, unsigned int track,
|
||||||
|
unsigned int * count);
|
||||||
|
|
||||||
|
/** Get a pointer to chunk number @a item of codec initialization data for
|
||||||
|
@a track.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@param item Zero based chunk item number.
|
||||||
|
@param data Storage for the queried data pointer.
|
||||||
|
The data is owned by the #nestegg context.
|
||||||
|
@param length Storage for the queried data size.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_codec_data(nestegg * context, unsigned int track, unsigned int item,
|
||||||
|
unsigned char ** data, size_t * length);
|
||||||
|
|
||||||
|
/** Query the video parameters specified by @a track.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@param params Storage for the queried video parameters.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_video_params(nestegg * context, unsigned int track,
|
||||||
|
nestegg_video_params * params);
|
||||||
|
|
||||||
|
/** Query the audio parameters specified by @a track.
|
||||||
|
@param context Stream context initialized by #nestegg_init.
|
||||||
|
@param track Zero based track number.
|
||||||
|
@param params Storage for the queried audio parameters.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_track_audio_params(nestegg * context, unsigned int track,
|
||||||
|
nestegg_audio_params * params);
|
||||||
|
|
||||||
|
/** Read a packet of media data. A packet consists of one or more chunks of
|
||||||
|
data associated with a single track. nestegg_read_packet should be
|
||||||
|
called in a loop while the return value is 1 to drive the stream parser
|
||||||
|
forward. @see nestegg_free_packet
|
||||||
|
@param context Context returned by #nestegg_init.
|
||||||
|
@param packet Storage for the returned nestegg_packet.
|
||||||
|
@retval 1 Additional packets may be read in subsequent calls.
|
||||||
|
@retval 0 End of stream.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_read_packet(nestegg * context, nestegg_packet ** packet);
|
||||||
|
|
||||||
|
/** Destroy a nestegg_packet and free associated memory.
|
||||||
|
@param packet #nestegg_packet to be freed. @see nestegg_read_packet */
|
||||||
|
void nestegg_free_packet(nestegg_packet * packet);
|
||||||
|
|
||||||
|
/** Query the track number of @a packet.
|
||||||
|
@param packet Packet initialized by #nestegg_read_packet.
|
||||||
|
@param track Storage for the queried zero based track index.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_packet_track(nestegg_packet * packet, unsigned int * track);
|
||||||
|
|
||||||
|
/** Query the time stamp in nanoseconds of @a packet.
|
||||||
|
@param packet Packet initialized by #nestegg_read_packet.
|
||||||
|
@param tstamp Storage for the queried timestamp in nanoseconds.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_packet_tstamp(nestegg_packet * packet, uint64_t * tstamp);
|
||||||
|
|
||||||
|
/** Query the number of data chunks contained in @a packet.
|
||||||
|
@param packet Packet initialized by #nestegg_read_packet.
|
||||||
|
@param count Storage for the queried timestamp in nanoseconds.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_packet_count(nestegg_packet * packet, unsigned int * count);
|
||||||
|
|
||||||
|
/** Get a pointer to chunk number @a item of packet data.
|
||||||
|
@param packet Packet initialized by #nestegg_read_packet.
|
||||||
|
@param item Zero based chunk item number.
|
||||||
|
@param data Storage for the queried data pointer.
|
||||||
|
The data is owned by the #nestegg_packet packet.
|
||||||
|
@param length Storage for the queried data size.
|
||||||
|
@retval 0 Success.
|
||||||
|
@retval -1 Error. */
|
||||||
|
int nestegg_packet_data(nestegg_packet * packet, unsigned int item,
|
||||||
|
unsigned char ** data, size_t * length);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* NESTEGG_671cac2a_365d_ed69_d7a3_4491d3538d79 */
|
Loading…
Reference in a new issue