Introducing mavl and mbag.

FossilOrigin-Name: 383bc0c7d65716c38a10aea87ab94c240e581cd2193f55fdfcbe644ef3dec0ff
This commit is contained in:
7u83@mail.ru
2015-04-19 21:27:44 +00:00
parent 341e098a6d
commit 381c910eec
46 changed files with 772 additions and 987 deletions

View File

@ -28,6 +28,7 @@
#include <stdint.h>
#include "mavl.h"
#include "bstr.h"
struct mbag_item;
@ -36,6 +37,8 @@ struct mbag_typedef{
void (*del)(void*);
};
typedef const struct mbagtype_def * mbagtype_t;
struct mbag_item{
@ -60,6 +63,11 @@ extern const struct mbag_typedef mbag_type_dword;
extern const struct mbag_typedef mbag_type_mbag;
extern const struct mbag_typedef mbag_type_bstr;
extern const struct mbag_typedef mbag_type_bstr16;
extern const struct mbag_typedef mbag_type_vendorstr;
extern const struct mbag_typedef mbag_type_str;
extern const struct mbag_typedef mbag_type_avltree;
extern const struct mbag_typedef mbag_type_const_data;
#define MBAG_BYTE (&mbag_type_byte)
@ -68,7 +76,13 @@ extern const struct mbag_typedef mbag_type_bstr16;
#define MBAG_MBAG (&mbag_type_mbag)
#define MBAG_BSTR (&mbag_type_bstr)
#define MBAG_BSTR16 (&mbag_type_bstr16)
#define MBAG_VENDORSTR (&mbag_type_vendorstr)
#define MBAG_STR (&mbag_type_str)
#define MBAG_DATA MBAG_STR
#define MBAG_AVLTREE (&mbag_type_avltree)
#define MBAG_FUN MBAG_STR
#define MBAG_CONST_DATA (&mbag_type_const_data)
extern mbag_t mbag_create();
@ -121,7 +135,7 @@ static inline int mbag_set_mbag(mbag_t s, uint32_t id, mbag_t mbag)
return 1;
}
static inline int mbag_set_data(mbag_t s, uint32_t id, struct mbag_typedef *type, void *data)
static inline int mbag_set_data(mbag_t s, uint32_t id, const struct mbag_typedef *type, void *data)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
@ -133,6 +147,23 @@ static inline int mbag_set_data(mbag_t s, uint32_t id, struct mbag_typedef *type
}
static inline int mbag_set_vendorstr(mbag_t s, uint32_t id, uint32_t vendor_id,
uint8_t * vendorstr, int len)
{
mbag_item_t *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_VENDORSTR;
i->data = vendorstr_create(vendor_id,vendorstr,len);
return 1;
}
#define mbag_set_bstr(m,i,d) mbag_set_data(m,i,MBAG_BSTR,d)
#define mbag_set_bstr16(m,i,d) mbag_set_data(m,i,MBAG_BSTR16,d)
@ -218,9 +249,183 @@ static inline mbag_t mbag_get_mbag(mbag_t s, uint32_t id, mbag_t def)
return i->data;
}
static inline mbag_item_t * mbag_set_bstrn(mbag_t s, uint32_t id, uint8_t * data, int len)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return NULL;
i->type = MBAG_BSTR;
i->data = bstr_create(data, len);
return i;
}
static inline struct avltree *mbag_get_avltree(mbag_t s, uint32_t id)
{
struct mbag_item *i = mbag_get(s, id);
if (!i)
return NULL;
if (i->type != MBAG_AVLTREE)
return NULL;
return i->data;
}
static inline int mbag_set_avltree(mbag_t s, uint32_t id, struct avltree *t)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_AVLTREE;
i->data = t;
printf("Adding AVL %p to %p\n",t,s);
return 1;
}
static inline struct avltree *mbag_get_avltree_c(mbag_t s, uint32_t id,
struct avltree *(creator) ())
{
struct avltree *avltree = mbag_get_avltree(s, id);
if (avltree){
return avltree;
}
avltree = creator();
if (!avltree)
return NULL;
mbag_set_avltree(s, id, avltree);
return avltree;
}
/** @} */
static inline char *mbag_get_str(mbag_t s, uint32_t id, char *def)
{
struct mbag_item *i = mbag_get(s, id);
if (!i)
return def;
return i->data;
};
static inline int mbag_set_str(mbag_t s, uint32_t id, const char *str)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_STR;
i->data = strdup(str);
return 1;
}
struct mbag_item_fundef {
void *(*get) (void *arg);
void (*free) (void *arg, void *data);
void *arg;
};
static inline void *mbag_item_get_data_ptr(struct mbag_item *item)
{
if (item->type == MBAG_FUN )
{
struct mbag_item_fundef *fundef =
(struct mbag_item_fundef *) item->data;
if (!fundef)
return NULL;
return fundef->get(fundef->arg);
}
return item->data;
}
static inline void mbag_item_release_data_ptr(struct mbag_item *item, void *data)
{
if (item->type == MBAG_FUN)
{
struct mbag_item_fundef *fundef =
(struct mbag_item_fundef *) item->data;
if (!fundef)
return;
if (!fundef->free)
return;
return fundef->free(fundef->arg, data);
}
}
static inline int mbag_set_strn(mbag_t s, uint32_t id, const char *str, int n)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_STR;
i->data = strndup(str, n);
return 1;
}
static inline int mbag_set_bstr16n(mbag_t s, uint32_t id, uint8_t * data, int len)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_BSTR16;
i->data = bstr16_create(data, len);
return 1;
}
static inline mbag_item_t * mbag_set_const_ptr(mbag_t s, uint32_t id, void *ptr)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_CONST_DATA;
i->data = ptr;
return i;
}
static inline int mbag_set_ptr(mbag_t s, uint32_t id, void *ptr)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
i->type = MBAG_DATA;
i->data = ptr;
return 1;
}
static inline int mbag_set_fun(mbag_t s, uint32_t id,
void *(*funget) (void *arg),
void (*funfree) (void *arg, void *data), void *arg)
{
struct mbag_item *i = mbag_item_create(s, id);
if (!i)
return 0;
struct mbag_item_fundef *fundef = malloc(sizeof(struct mbag_item_fundef));
i->data = fundef;
if (!fundef)
return 0;
fundef->get = funget;
fundef->free = funfree;
fundef->arg = arg;
i->type=MBAG_FUN;
return 1;
}
#endif