From 335dd2eba57673f0e0b397fab20ff4be2f18cc3d Mon Sep 17 00:00:00 2001 From: "shaver%netscape.com" Date: Mon, 18 Jan 1999 18:28:14 +0000 Subject: [PATCH] Much XDR header whacking. Still need to figure out how the pool management stuff looks. Fixed XPT_PREAMBLE (again). xpt_xdr.c now compiles! --- xpcom/libxpt/public/xpt_xdr.h | 30 ++++---- xpcom/libxpt/src/xpt_xdr.c | 116 +++++++++++++++++------------ xpcom/typelib/xpt/public/xpt_xdr.h | 30 ++++---- xpcom/typelib/xpt/src/xpt_xdr.c | 116 +++++++++++++++++------------ 4 files changed, 168 insertions(+), 124 deletions(-) diff --git a/xpcom/libxpt/public/xpt_xdr.h b/xpcom/libxpt/public/xpt_xdr.h index 8d8d14542e0..b4f57acf243 100644 --- a/xpcom/libxpt/public/xpt_xdr.h +++ b/xpcom/libxpt/public/xpt_xdr.h @@ -91,9 +91,10 @@ struct XPTDatapool { struct XPTCursor { XPTState *state; - XPTPool pool; + XPTDatapool *pool; uint32 offset; uint32 len; + uint8 bits; }; XPTState * @@ -124,11 +125,6 @@ XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, uint32 *len); PRBool XPT_CreateCursor(XPTCursor *base, XPTPool pool, uint32 len, XPTCursor *cursor); -/* increase the data allocation for the pool by XPT_GROW_CHUNK */ -#define XPT_GROW_CHUNK 8192 -PRBool -XPT_GrowPool(XPTDatapool *pool); - /* all data structures are big-endian */ #if defined IS_BIG_ENDIAN @@ -156,8 +152,7 @@ XPT_GrowPool(XPTDatapool *pool); * mapping. */ -#define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, \ - ALLOC_CODE) { \ +#define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already) \ XPTMode mode = cursor->state->mode; \ if (!(mode == XPT_ENCODE || XPT_Do32(cursor, &new_curs.offset)) || \ !XPT_CheckForRepeat(cursor, (void **)addrp, pool, \ @@ -167,25 +162,28 @@ XPT_GrowPool(XPTDatapool *pool); return PR_FALSE; \ if (already) \ return PR_TRUE; \ - ALLOC_CODE; \ -} -#define XPT_ALLOC \ +#define XPT_ALLOC(addrp, new_curs, XPTType, localp) \ if (mode == XPT_DECODE) { \ *addrp = localp = PR_NEWZAP(XPTType); \ if (!localp || \ - !XPT_SetAddrForOffset(new_curs, localp) \ + !XPT_SetAddrForOffset(new_curs, localp)) \ return PR_FALSE; \ } else { \ localp = *addrp; \ } -#define XPT_PREAMBLE(cursor, addrp, pool, size, new_curs, already, XPTType, localp) \ - XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, XPT_ALLOC) +#define XPT_PREAMBLE(cursor, addrp, pool, size, new_curs, already, \ + XPTType, localp) \ + { \ + XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already); \ + XPT_ALLOC(addrp, new_curs, XPTType, localp) \ + } #define XPT_PREAMBLE_NO_ALLOC(cursor, addrp, pool, size, new_curs, already) \ - XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, ;) - + { \ + XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already) \ + } #define XPT_ERROR_HANDLE(free_it) \ error: \ diff --git a/xpcom/libxpt/src/xpt_xdr.c b/xpcom/libxpt/src/xpt_xdr.c index 4bdacc6e575..f71a70c6318 100644 --- a/xpcom/libxpt/src/xpt_xdr.c +++ b/xpcom/libxpt/src/xpt_xdr.c @@ -19,44 +19,65 @@ /* Implementation of XDR primitives. */ #include "xpt_xdr.h" +#include /* strchr */ -#define CHECK_COUNT(state) \ - ((state)->pool->count = (state)->pool->allocated ? \ - ((state)->mode == XPT_ENCODE ? XPT_GrowPool((state)->pool) : \ +#define CHECK_COUNT(cursor) \ + ((cursor)->offset == (cursor)->pool->allocated ? \ + ((cursor)->state->mode == XPT_ENCODE ? XPT_GrowPool((cursor)->pool) : \ PR_FALSE) : PR_TRUE) +/* increase the data allocation for the pool by XPT_GROW_CHUNK */ +#define XPT_GROW_CHUNK 8192 + XPTState * XPT_NewXDRState(XPTMode mode, char *data, uint32 len) { +#if 0 /* need to rethink pool management */ XPTState *state; + int i; + state = PR_NEW(XPTState); if (!state) return NULL; state->mode = mode; - state->pool = PR_NEW(XPTDatapool); - if (!state->pool) { - PR_FREE(state); - return NULL; - } - state->pool->count = 0; - state->pool->bit = 0; - if (mode == XPT_DECODE) { - state->pool->data = data; - state->pool->allocated = len; - } else { - state->pool->data = PR_MALLOC(XPT_GROW_CHUNK); - if (!state->pool->data) { - PR_FREE(state->pool); + for (i = 0; i < 2; i++) { + state->pools[i] = PR_NEW(XPTDatapool); + if (!state->pools[i]) { + if (i) { + PR_FREE(state->pools[0]->data); + PR_FREE(state->pools[0]); + } PR_FREE(state); return NULL; } - state->data->allocated = XPT_GROW_CHUNK; + state->pools[i]->count = 0; + state->pools[i]->bit = 0; + if (mode == XPT_DECODE) { + state->pools[i]->data = data; + state->pools[i]->allocated = len; + } else { + state->pools[i]->data = PR_MALLOC(XPT_GROW_CHUNK); + if (!state->pools[i]->data) { + PR_FREE(state->pools[i]); + if (i) { + PR_FREE(state->pools[0]->data); + PR_FREE(state->pools[0]); + } + PR_FREE(state); + return NULL; + } + state->pools[i]->allocated = XPT_GROW_CHUNK; + } } +#endif + return NULL; } void XPT_DestroyXDRState(XPTState *state) { +#if 0 /* need to rethink pool management */ + int i; if (state->mode == XPT_ENCODE) { PR_FREE_IF(state->pool->data); PR_FREE(state->pool); @@ -65,13 +86,14 @@ XPT_DestroyXDRState(XPTState *state) PR_FREE(state->pool); PR_FREE(state); } +#endif } void -XPT_GetXDRData(XPTState *state, char **data, uint32 *len) +XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, uint32 *len) { - *data = state->pool->data; - *len = state->pool->count; + *data = state->pools[pool]->data; + *len = state->pools[pool]->count; } static PRBool @@ -91,6 +113,7 @@ XPT_DoString(XPTCursor *cursor, XPTString **strp) XPTCursor my_cursor; XPTString *str = *strp; PRBool already; + int i; XPT_PREAMBLE(cursor, strp, XPT_DATA, str->length + 2, my_cursor, already, XPTString, str); @@ -113,12 +136,13 @@ XPT_DoCString(XPTCursor *cursor, char **identp) XPTCursor my_cursor; char *ident = *identp; PRBool already; + XPTMode mode = cursor->state->mode; XPT_PREAMBLE_NO_ALLOC(cursor, identp, XPT_DATA, strlen(ident) + 1, - my_cursor, already, ident); + my_cursor, already); if (mode == XPT_DECODE) { - char *start = my_cursor->state->data[my_cursor->offset], *end; + char *start = &my_cursor.pool->data[my_cursor.offset], *end; int len; end = strchr(start, 0); /* find the end of the string */ @@ -136,7 +160,7 @@ XPT_DoCString(XPTCursor *cursor, char **identp) ident[len] = 0; *identp = ident; - if (!XPT_SetAddrForOffset(my_cursor, my_cursor->offset, ident)) { + if (!XPT_SetAddrForOffset(&my_cursor, my_cursor.offset, ident)) { PR_FREE(ident); return PR_FALSE; } @@ -159,7 +183,7 @@ XPT_GetOffsetForAddr(XPTCursor *cursor, void *addr) } PRBool -XPT_SetOffsetForAddr(XPTState *state, uint32 offset, void **addr) +XPT_SetOffsetForAddr(XPTCursor *cursor, void **addr, uint32 offset) { *addr = NULL; return PR_FALSE; @@ -185,7 +209,7 @@ XPT_CheckForRepeat(XPTCursor *cursor, void **addrp, XPTPool pool, int len, *already = PR_FALSE; new_cursor->state = cursor->state; - new_cursor->pool = pool; + new_cursor->pool = cursor->state->pools[pool]; if (cursor->state->mode = XPT_DECODE) { @@ -206,7 +230,7 @@ XPT_CheckForRepeat(XPTCursor *cursor, void **addrp, XPTPool pool, int len, /* haven't already found it, so allocate room for it. */ if (!XPT_AllocateCursor(cursor, pool, len, new_cursor) || - !XPT_SetOffsetForAddr(new_cursor, last)) + !XPT_SetOffsetForAddr(new_cursor, *addrp, new_cursor->offset)) return PR_FALSE; } return PR_TRUE; @@ -254,9 +278,9 @@ static PRBool do_bit(XPTCursor *cursor, uint8 *u8p, int bitno) { int bit_value, delta, new_value; - XPTDatapool *pool = state->pool; + XPTDatapool *pool = cursor->pool; - if (state->mode == XPT_ENCODE) { + if (cursor->state->mode == XPT_ENCODE) { bit_value = (*u8p & 1) << (bitno); /* 7 = 0100 0000, 6 = 0010 0000 */ if (bit_value) { delta = pool->bit + (bitno) - 7; @@ -265,39 +289,39 @@ do_bit(XPTCursor *cursor, uint8 *u8p, int bitno) } } else { bit_value = pool->data[pool->count] & (1 << (7 - pool->bit)); - *u2p = bit_value >> (7 - pool->bit); + *u8p = bit_value >> (7 - pool->bit); } if (++pool->bit == 8) { pool->count++; pool->bit = 0; } - return CHECK_COUNT(state); + return CHECK_COUNT(cursor); } -int -XPT_DoBits(XPTCursor *cursor, uint8 *u8p, uintN nbits) +PRBool +XPT_DoBits(XPTCursor *cursor, uint8 *u8p, int nbits) { -#define DO_BIT(state, u8p, nbits) \ - if (!do_bit(state, u8p, nbits)) \ +#define DO_BIT(cursor, u8p, nbits) \ + if (!do_bit(cursor, u8p, nbits)) \ return PR_FALSE; switch(nbits) { case 7: - DO_BIT(state, u8p, 7); + DO_BIT(cursor, u8p, 7); case 6: - DO_BIT(state, u8p, 6); + DO_BIT(cursor, u8p, 6); case 5: - DO_BIT(state, u8p, 5); + DO_BIT(cursor, u8p, 5); case 4: - DO_BIT(state, u8p, 4); + DO_BIT(cursor, u8p, 4); case 3: - DO_BIT(state, u8p, 3); + DO_BIT(cursor, u8p, 3); case 2: - DO_BIT(state, u8p, 2); + DO_BIT(cursor, u8p, 2); case 1: - DO_BIT(state, u8p, 1); + DO_BIT(cursor, u8p, 1); default:; }; @@ -309,12 +333,12 @@ XPT_DoBits(XPTCursor *cursor, uint8 *u8p, uintN nbits) int XPT_FlushBits(XPTCursor *cursor) { - int skipped = 8 - state->pool->bits; + int skipped = 8 - cursor->bits; - state->pool->bits = 0; - state->count++; + cursor->bits = 0; + cursor->offset++; - if (!CHECK_COUNT(state)) + if (!CHECK_COUNT(cursor)) return -1; return skipped == 8 ? 0 : skipped; diff --git a/xpcom/typelib/xpt/public/xpt_xdr.h b/xpcom/typelib/xpt/public/xpt_xdr.h index 8d8d14542e0..b4f57acf243 100644 --- a/xpcom/typelib/xpt/public/xpt_xdr.h +++ b/xpcom/typelib/xpt/public/xpt_xdr.h @@ -91,9 +91,10 @@ struct XPTDatapool { struct XPTCursor { XPTState *state; - XPTPool pool; + XPTDatapool *pool; uint32 offset; uint32 len; + uint8 bits; }; XPTState * @@ -124,11 +125,6 @@ XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, uint32 *len); PRBool XPT_CreateCursor(XPTCursor *base, XPTPool pool, uint32 len, XPTCursor *cursor); -/* increase the data allocation for the pool by XPT_GROW_CHUNK */ -#define XPT_GROW_CHUNK 8192 -PRBool -XPT_GrowPool(XPTDatapool *pool); - /* all data structures are big-endian */ #if defined IS_BIG_ENDIAN @@ -156,8 +152,7 @@ XPT_GrowPool(XPTDatapool *pool); * mapping. */ -#define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, \ - ALLOC_CODE) { \ +#define XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already) \ XPTMode mode = cursor->state->mode; \ if (!(mode == XPT_ENCODE || XPT_Do32(cursor, &new_curs.offset)) || \ !XPT_CheckForRepeat(cursor, (void **)addrp, pool, \ @@ -167,25 +162,28 @@ XPT_GrowPool(XPTDatapool *pool); return PR_FALSE; \ if (already) \ return PR_TRUE; \ - ALLOC_CODE; \ -} -#define XPT_ALLOC \ +#define XPT_ALLOC(addrp, new_curs, XPTType, localp) \ if (mode == XPT_DECODE) { \ *addrp = localp = PR_NEWZAP(XPTType); \ if (!localp || \ - !XPT_SetAddrForOffset(new_curs, localp) \ + !XPT_SetAddrForOffset(new_curs, localp)) \ return PR_FALSE; \ } else { \ localp = *addrp; \ } -#define XPT_PREAMBLE(cursor, addrp, pool, size, new_curs, already, XPTType, localp) \ - XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, XPT_ALLOC) +#define XPT_PREAMBLE(cursor, addrp, pool, size, new_curs, already, \ + XPTType, localp) \ + { \ + XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already); \ + XPT_ALLOC(addrp, new_curs, XPTType, localp) \ + } #define XPT_PREAMBLE_NO_ALLOC(cursor, addrp, pool, size, new_curs, already) \ - XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already, ;) - + { \ + XPT_PREAMBLE_(cursor, addrp, pool, size, new_curs, already) \ + } #define XPT_ERROR_HANDLE(free_it) \ error: \ diff --git a/xpcom/typelib/xpt/src/xpt_xdr.c b/xpcom/typelib/xpt/src/xpt_xdr.c index 4bdacc6e575..f71a70c6318 100644 --- a/xpcom/typelib/xpt/src/xpt_xdr.c +++ b/xpcom/typelib/xpt/src/xpt_xdr.c @@ -19,44 +19,65 @@ /* Implementation of XDR primitives. */ #include "xpt_xdr.h" +#include /* strchr */ -#define CHECK_COUNT(state) \ - ((state)->pool->count = (state)->pool->allocated ? \ - ((state)->mode == XPT_ENCODE ? XPT_GrowPool((state)->pool) : \ +#define CHECK_COUNT(cursor) \ + ((cursor)->offset == (cursor)->pool->allocated ? \ + ((cursor)->state->mode == XPT_ENCODE ? XPT_GrowPool((cursor)->pool) : \ PR_FALSE) : PR_TRUE) +/* increase the data allocation for the pool by XPT_GROW_CHUNK */ +#define XPT_GROW_CHUNK 8192 + XPTState * XPT_NewXDRState(XPTMode mode, char *data, uint32 len) { +#if 0 /* need to rethink pool management */ XPTState *state; + int i; + state = PR_NEW(XPTState); if (!state) return NULL; state->mode = mode; - state->pool = PR_NEW(XPTDatapool); - if (!state->pool) { - PR_FREE(state); - return NULL; - } - state->pool->count = 0; - state->pool->bit = 0; - if (mode == XPT_DECODE) { - state->pool->data = data; - state->pool->allocated = len; - } else { - state->pool->data = PR_MALLOC(XPT_GROW_CHUNK); - if (!state->pool->data) { - PR_FREE(state->pool); + for (i = 0; i < 2; i++) { + state->pools[i] = PR_NEW(XPTDatapool); + if (!state->pools[i]) { + if (i) { + PR_FREE(state->pools[0]->data); + PR_FREE(state->pools[0]); + } PR_FREE(state); return NULL; } - state->data->allocated = XPT_GROW_CHUNK; + state->pools[i]->count = 0; + state->pools[i]->bit = 0; + if (mode == XPT_DECODE) { + state->pools[i]->data = data; + state->pools[i]->allocated = len; + } else { + state->pools[i]->data = PR_MALLOC(XPT_GROW_CHUNK); + if (!state->pools[i]->data) { + PR_FREE(state->pools[i]); + if (i) { + PR_FREE(state->pools[0]->data); + PR_FREE(state->pools[0]); + } + PR_FREE(state); + return NULL; + } + state->pools[i]->allocated = XPT_GROW_CHUNK; + } } +#endif + return NULL; } void XPT_DestroyXDRState(XPTState *state) { +#if 0 /* need to rethink pool management */ + int i; if (state->mode == XPT_ENCODE) { PR_FREE_IF(state->pool->data); PR_FREE(state->pool); @@ -65,13 +86,14 @@ XPT_DestroyXDRState(XPTState *state) PR_FREE(state->pool); PR_FREE(state); } +#endif } void -XPT_GetXDRData(XPTState *state, char **data, uint32 *len) +XPT_GetXDRData(XPTState *state, XPTPool pool, char **data, uint32 *len) { - *data = state->pool->data; - *len = state->pool->count; + *data = state->pools[pool]->data; + *len = state->pools[pool]->count; } static PRBool @@ -91,6 +113,7 @@ XPT_DoString(XPTCursor *cursor, XPTString **strp) XPTCursor my_cursor; XPTString *str = *strp; PRBool already; + int i; XPT_PREAMBLE(cursor, strp, XPT_DATA, str->length + 2, my_cursor, already, XPTString, str); @@ -113,12 +136,13 @@ XPT_DoCString(XPTCursor *cursor, char **identp) XPTCursor my_cursor; char *ident = *identp; PRBool already; + XPTMode mode = cursor->state->mode; XPT_PREAMBLE_NO_ALLOC(cursor, identp, XPT_DATA, strlen(ident) + 1, - my_cursor, already, ident); + my_cursor, already); if (mode == XPT_DECODE) { - char *start = my_cursor->state->data[my_cursor->offset], *end; + char *start = &my_cursor.pool->data[my_cursor.offset], *end; int len; end = strchr(start, 0); /* find the end of the string */ @@ -136,7 +160,7 @@ XPT_DoCString(XPTCursor *cursor, char **identp) ident[len] = 0; *identp = ident; - if (!XPT_SetAddrForOffset(my_cursor, my_cursor->offset, ident)) { + if (!XPT_SetAddrForOffset(&my_cursor, my_cursor.offset, ident)) { PR_FREE(ident); return PR_FALSE; } @@ -159,7 +183,7 @@ XPT_GetOffsetForAddr(XPTCursor *cursor, void *addr) } PRBool -XPT_SetOffsetForAddr(XPTState *state, uint32 offset, void **addr) +XPT_SetOffsetForAddr(XPTCursor *cursor, void **addr, uint32 offset) { *addr = NULL; return PR_FALSE; @@ -185,7 +209,7 @@ XPT_CheckForRepeat(XPTCursor *cursor, void **addrp, XPTPool pool, int len, *already = PR_FALSE; new_cursor->state = cursor->state; - new_cursor->pool = pool; + new_cursor->pool = cursor->state->pools[pool]; if (cursor->state->mode = XPT_DECODE) { @@ -206,7 +230,7 @@ XPT_CheckForRepeat(XPTCursor *cursor, void **addrp, XPTPool pool, int len, /* haven't already found it, so allocate room for it. */ if (!XPT_AllocateCursor(cursor, pool, len, new_cursor) || - !XPT_SetOffsetForAddr(new_cursor, last)) + !XPT_SetOffsetForAddr(new_cursor, *addrp, new_cursor->offset)) return PR_FALSE; } return PR_TRUE; @@ -254,9 +278,9 @@ static PRBool do_bit(XPTCursor *cursor, uint8 *u8p, int bitno) { int bit_value, delta, new_value; - XPTDatapool *pool = state->pool; + XPTDatapool *pool = cursor->pool; - if (state->mode == XPT_ENCODE) { + if (cursor->state->mode == XPT_ENCODE) { bit_value = (*u8p & 1) << (bitno); /* 7 = 0100 0000, 6 = 0010 0000 */ if (bit_value) { delta = pool->bit + (bitno) - 7; @@ -265,39 +289,39 @@ do_bit(XPTCursor *cursor, uint8 *u8p, int bitno) } } else { bit_value = pool->data[pool->count] & (1 << (7 - pool->bit)); - *u2p = bit_value >> (7 - pool->bit); + *u8p = bit_value >> (7 - pool->bit); } if (++pool->bit == 8) { pool->count++; pool->bit = 0; } - return CHECK_COUNT(state); + return CHECK_COUNT(cursor); } -int -XPT_DoBits(XPTCursor *cursor, uint8 *u8p, uintN nbits) +PRBool +XPT_DoBits(XPTCursor *cursor, uint8 *u8p, int nbits) { -#define DO_BIT(state, u8p, nbits) \ - if (!do_bit(state, u8p, nbits)) \ +#define DO_BIT(cursor, u8p, nbits) \ + if (!do_bit(cursor, u8p, nbits)) \ return PR_FALSE; switch(nbits) { case 7: - DO_BIT(state, u8p, 7); + DO_BIT(cursor, u8p, 7); case 6: - DO_BIT(state, u8p, 6); + DO_BIT(cursor, u8p, 6); case 5: - DO_BIT(state, u8p, 5); + DO_BIT(cursor, u8p, 5); case 4: - DO_BIT(state, u8p, 4); + DO_BIT(cursor, u8p, 4); case 3: - DO_BIT(state, u8p, 3); + DO_BIT(cursor, u8p, 3); case 2: - DO_BIT(state, u8p, 2); + DO_BIT(cursor, u8p, 2); case 1: - DO_BIT(state, u8p, 1); + DO_BIT(cursor, u8p, 1); default:; }; @@ -309,12 +333,12 @@ XPT_DoBits(XPTCursor *cursor, uint8 *u8p, uintN nbits) int XPT_FlushBits(XPTCursor *cursor) { - int skipped = 8 - state->pool->bits; + int skipped = 8 - cursor->bits; - state->pool->bits = 0; - state->count++; + cursor->bits = 0; + cursor->offset++; - if (!CHECK_COUNT(state)) + if (!CHECK_COUNT(cursor)) return -1; return skipped == 8 ? 0 : skipped;