📄 exp_buf.c
字号:
/* * exp_buf.c - buffer routines for the buffer structure * * * --------- ---------- * | ExpBuf |<------>| ExpBuf |<------> ...ExpBufs * | |--- | |--- * ---------- | ---------- | * V V * -------- -------- * | DATA | | DATA | * | BLK | | BLK | * -------- -------- * * * ExpBuf * -------------- * | readError | * | writeError | * | dataStart |----------- * | dataEnd |-------- | * | curr |------ | | * | next | | | | * | prev | | | | data * | blkStart |=====|=|==|==>-------------------------- * | blkEnd |--- | | | | | (each line * -------------- | | | | | | reps a byte * | | | |-->| - - - - - - - - - - - -| diff in addr) * | | | | valid | * | |-|----->| | * | | | data | * | | | | * | | | - - - - - - - - - - - -| * | |----->|(one byte after last valid data byte) * | | | * | -------------------------- * |-----------> (one byte after last byte in data blk) * * * readError - set to non-zero to indicate attempt to read past end of * of data * writeError- set to non-zero to indicate write error. * Set if Alloc of new buf fails * dataStart - pts to first VALID data byte ie *dataStart is first byte * dataEnd - pts to byte AFTER last VALID byte *dataEnd is not in the data * but *(dataEnd -1) is in the data * curr - used for current read ptr - points to next byte to be read * so *curr is the next byte to be read. * next - pts to next BUF in list, NULL for last BUF in list * prev - pts to prev BUF in list, NULL for first BUF in list * blkStart - pts to start of the data blk. *blkStart is first byte * in the buffer's data blk. * blkEnd - pts to byte AFTER last writable byte of the dataBlk. * *(blkEnd-1) is the last byte in the buffer's data blk. * * NOTES: * - dataEnd is currently always the same as blkEnd * - at End Of Data (EOD) (no more data to be read) * if (curr == dataEnd) * - buffer has no valid data if (dataStart == dataEnd) * - number of valid data bytes = (dataEnd - dataStart) * - size of the data block = (blkEnd - blkStart) * * - the write reverse routines modify dataStart * - the read routines modify the curr ptr. * - there are no 'forward' write routines at the moment * (if there were they would adjust dataEnd) * * * Copyright (C) 1992 Michael Sample and the University of British Columbia * * This library is free software; you can redistribute it and/or * modify it provided that this copyright/license information is retained * in original form. * * If you modify this file, you must clearly indicate your changes. * * This source code is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * */ #if HAVE_CONFIG_H#include <config.h>#endif #include "asn_config.h"#include "exp_buf.h"/* default buffer data block size (used when allocating) */unsigned long expBufDataBlkSizeG = 1024;#ifdef DEBUG /* otherwise macros *//* * sets the size of the data block to attach to * an ExpBuf when allocating a new one */voidExpBufInit PARAMS((dataBlkSize),unsigned long dataBlkSize){ expBufDataBlkSizeG = dataBlkSize;} /* InitBuffers *//* * Allocates and returns an uninitialized ExpBuf with * no a data attached. */ExpBuf*ExpBufAllocBuf(){ return ((ExpBuf*)malloc(sizeof(ExpBuf)));}voidExpBufFreeBuf PARAMS( (ptr),ExpBuf* ptr){ free(ptr);}char*ExpBufAllocData(){ return((char*)malloc(expBufDataBlkSizeG));}voidExpBufFreeData PARAMS( (ptr),char* ptr){ free( ptr);}ExpBuf*ExpBufNext PARAMS( (b),ExpBuf* b){ return(b->next);}ExpBuf*ExpBufPrev PARAMS( (b),ExpBuf* b){ return(b->prev);}/* * set curr ptr used in reads to the first byte * to be read */voidExpBufResetInReadMode PARAMS((b),ExpBuf* b){ b->curr = b->dataStart; b->readError = 0; b->writeError = 1; /* catch wrong mode errors */}/* * sets dataStart to end of buffer * so following writes (backward) * over-write any existing data associated with * the buffer */voidExpBufResetInWriteRvsMode PARAMS((b),ExpBuf* b){ b->dataEnd = b->dataStart = b->blkEnd; b->writeError = 0; b->readError = 1; /* catch wrong mode errors */}/* * returns true if no more data can be read from * the given buffer. only valid when buffer in read (fwd) * mode. */intExpBufAtEod PARAMS((b),ExpBuf* b){ return(b->curr == b->dataEnd);}/* * returns true if no more reverse writes can be done * to the buffer. Only valid when buffers in reverse * write mode */intExpBufFull PARAMS((b),ExpBuf* b){ return((b)->dataStart == (b)->blkStart);}/* * returns true if the given buffer has no * valid data in it's data block */intExpBufHasNoData PARAMS((b),ExpBuf* b){ return(b->dataStart == b->dataEnd);}/* * returns the number of valid data bytes in the * given buffer's data block */unsigned longExpBufDataSize PARAMS((b),ExpBuf* b){ return(b->dataEnd - b->dataStart);}/* * returns size of data block that is attached to * the given buffer. */unsigned longExpBufDataBlkSize PARAMS((b),ExpBuf* b){ return(b->blkEnd - b->blkStart);}/* * returns a ptr the beginning of the valid data of * the given buffer. * returns NULL is there is no valid data. */char*ExpBufDataPtr PARAMS((b),ExpBuf* b){ if (ExpBufHasNoData(b)) return(NULL); else return(b->dataStart);}#endif /* DEBUG *//* * returns last ExpBuf in a list of bufs. * The given buf can be any buf in the list. */ExpBuf*ExpBufListLastBuf PARAMS((b),ExpBuf* b){ for (; b->next != NULL; b = b->next); return(b);}/* * returns first buf in a list of bufs . * The given buf can be any buf in the list */ExpBuf*ExpBufListFirstBuf PARAMS((b),ExpBuf* b){ for (; b->prev != NULL; b = b->prev); return(b);}/* * Allocates a Buf and allocates an attaches a * data block of expBufDataBlkSizeG to that buffer. * sets up the blk for writing in that the data start * and data end point to the byte after the data blk. */ExpBuf*ExpBufAllocBufAndData(){ ExpBuf* retVal; retVal = ExpBufAllocBuf(); if (retVal == NULL) return(NULL); retVal->readError = 0; retVal->writeError = 0; retVal->blkStart = ExpBufAllocData(); if (retVal->blkStart == NULL) { ExpBufFreeBuf(retVal); return(NULL); } retVal->next = NULL; retVal->prev = NULL; retVal->curr = retVal->blkEnd = retVal->dataStart = retVal->dataEnd = retVal->blkStart + expBufDataBlkSizeG; return(retVal);} /* ExpBufAllocBufAndData *//* * Frees ExpBuf's and associated data blocks after * after (next ptr) and including the given buffer, b. */voidExpBufFreeBufAndDataList PARAMS( (b),ExpBuf* b){ ExpBuf* tmp; for(; b != NULL;) { tmp = b->next; ExpBufFreeBufAndData(b); b = tmp; }} /* ExpBufFreeBufAndDataList */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -