Major cleanup of comments

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@128 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Dave Beazley 2000-01-20 05:45:58 +00:00
commit d72c7beb9d
25 changed files with 769 additions and 853 deletions

View file

@ -1,21 +1,17 @@
/******************************************************************************
* DOH
/* -----------------------------------------------------------------------------
* base.c
*
* This file contains the function entry points for dispatching methods on
* DOH objects. A number of small utility functions are also included.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
******************************************************************************/
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
/*******************************************************************************
* base.c
*
* This file contains the function entry points for dispatching methods on DOH
* objects.
*******************************************************************************/
#include "dohint.h"
static DohObjInfo DohBaseType = {
@ -1096,6 +1092,54 @@ DohInit(DOH *b) {
bs->flags = 0;
}
/* -----------------------------------------------------------------------------
* DohXBase_setfile()
*
* Set file location (default method).
* ----------------------------------------------------------------------------- */
void
DohXBase_setfile(DOH *ho, DOH *file) {
DohXBase *h = (DohXBase *) ho;
if (!DohCheck(file)) file = NewString(file);
h->file = file;
Incref(h->file);
}
/* -----------------------------------------------------------------------------
* DohXBase_getfile()
* ----------------------------------------------------------------------------- */
DOH *
DohXBase_getfile(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->file;
}
/* -----------------------------------------------------------------------------
* DohXBase_setline()
* ----------------------------------------------------------------------------- */
void
DohXBase_setline(DOH *ho, int l) {
DohXBase *h = (DohXBase *) ho;
h->line = l;
}
/* -----------------------------------------------------------------------------
* DohXBase_getline()
* ----------------------------------------------------------------------------- */
int
DohXBase_getline(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->line;
}
static DohPositionalMethods XBasePositionalMethods = {
DohXBase_setfile,
DohXBase_getfile,
DohXBase_setline,
DohXBase_getline
};
/* -----------------------------------------------------------------------------
* DohXInit()
*
@ -1106,47 +1150,9 @@ DohXInit(DOH *b) {
DohXBase *bs = (DohXBase *) b;
bs->file = 0;
bs->line = 0;
}
/* -----------------------------------------------------------------------------
* XBase_setfile()
*
* Set file location (default method).
* ----------------------------------------------------------------------------- */
void
XBase_setfile(DOH *ho, DOH *file) {
DohXBase *h = (DohXBase *) ho;
if (!DohCheck(file)) file = NewString(file);
h->file = file;
Incref(h->file);
}
/* -----------------------------------------------------------------------------
* XBase_getfile()
* ----------------------------------------------------------------------------- */
DOH *
XBase_getfile(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->file;
}
/* -----------------------------------------------------------------------------
* XBase_setline()
* ----------------------------------------------------------------------------- */
void
XBase_setline(DOH *ho, int l) {
DohXBase *h = (DohXBase *) ho;
h->line = l;
}
/* -----------------------------------------------------------------------------
* XBase_getline()
* ----------------------------------------------------------------------------- */
int
XBase_getline(DOH *ho) {
DohXBase *h = (DohXBase *) ho;
return h->line;
bs->objinfo->doh_position = &XBasePositionalMethods;
}

View file

@ -1,20 +1,17 @@
/******************************************************************************
* DOH
/* -----------------------------------------------------------------------------
* callable.c
*
* This file implements a simple callable object supporting a function call
* operation.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
******************************************************************************/
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
/* -----------------------------------------------------------------------------
* callable.c
*
* Implements a callable function-like object.
* ----------------------------------------------------------------------------- */
#include "dohint.h"
typedef struct {

View file

@ -1,20 +1,17 @@
/******************************************************************************
* DOH
/* -----------------------------------------------------------------------------
* file.c
*
* This file implements a file-like object that can be built around an
* ordinary FILE * or integer file descriptor.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
******************************************************************************/
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
/* ---------------------------------------------------------------------------
* file.c
*
* This file implements a DOH file-like object.
* --------------------------------------------------------------------------- */
#include "dohint.h"
#include <unistd.h>
@ -26,7 +23,9 @@ typedef struct {
} DohFile;
/* -----------------------------------------------------------------------------
* DelFile(DOH *s) - Delete a file
* DelFile()
*
* Delete a file. Closes the file if the closeondel flag is set.
* ----------------------------------------------------------------------------- */
static void
@ -39,9 +38,9 @@ DelFile(DOH *so) {
}
/* -----------------------------------------------------------------------------
* int File_read(DOH *so, void *buffer, int len)
* File_read()
*
* Read data from the File
* Reads data from a file into a buffer.
* ----------------------------------------------------------------------------- */
static int
@ -54,10 +53,11 @@ File_read(DOH *so, void *buffer, int len) {
}
/* -----------------------------------------------------------------------------
* int File_write(DOH *so, void *buffer, int len)
* File_write()
*
* Write data to the File
* Write data from a buffer to a file.
* ----------------------------------------------------------------------------- */
static int
File_write(DOH *so, void *buffer, int len) {
DohFile *s = (DohFile *) so;
@ -68,10 +68,11 @@ File_write(DOH *so, void *buffer, int len) {
}
/* -----------------------------------------------------------------------------
* int File_seek(DOH *so, long offset, int whence)
* File_seek()
*
* Seek to a new position
* Seek to a new file position.
* ----------------------------------------------------------------------------- */
static int
File_seek(DOH *so, long offset, int whence) {
DohFile *s = (DohFile *) so;
@ -82,10 +83,11 @@ File_seek(DOH *so, long offset, int whence) {
}
/* -----------------------------------------------------------------------------
* long File_tell(DOH *so)
* File_tell()
*
* Return current position
* Return current file pointer.
* ----------------------------------------------------------------------------- */
static long
File_tell(DOH *so) {
DohFile *s = (DohFile *) so;
@ -96,7 +98,7 @@ File_tell(DOH *so) {
}
/* -----------------------------------------------------------------------------
* int File_putc(DOH *obj, int ch)
* File_putc()
*
* Put a character on the output
* ----------------------------------------------------------------------------- */
@ -110,7 +112,7 @@ File_putc(DOH *obj, int ch) {
}
/* -----------------------------------------------------------------------------
* int File_getc(DOH *obj)
* File_getc()
*
* Get a character
* ----------------------------------------------------------------------------- */
@ -124,7 +126,7 @@ File_getc(DOH *obj) {
}
/* -----------------------------------------------------------------------------
* int File_ungetc(DOH *obj, int ch)
* File_ungetc()
*
* Put a character back onto the input
* ----------------------------------------------------------------------------- */
@ -171,8 +173,11 @@ static DohObjInfo DohFileType = {
};
/* -----------------------------------------------------------------------------
* NewFile(DOH *filename, char *mode)
* NewFile()
*
* Create a new file from a given filename and mode.
* ----------------------------------------------------------------------------- */
DOH *
NewFile(DOH *fn, char *mode)
{
@ -197,7 +202,9 @@ NewFile(DOH *fn, char *mode)
}
/* -----------------------------------------------------------------------------
* NewFileFromFile(FILE *f)
* NewFileFromFile()
*
* Create a file object from an already open FILE *.
* ----------------------------------------------------------------------------- */
DOH *
@ -214,7 +221,9 @@ NewFileFromFile(FILE *file)
}
/* -----------------------------------------------------------------------------
* NewFileFromFd(int fd)
* NewFileFromFd()
*
* Create a file object from an already existing integer file descriptor.
* ----------------------------------------------------------------------------- */
DOH *
@ -232,7 +241,9 @@ NewFileFromFd(int fd)
/* -----------------------------------------------------------------------------
* int File_check(DOH *f) - Check if f is a file
* File_check()
*
* Check if an object is a file.
* ----------------------------------------------------------------------------- */
int
File_check(DOH *f)

View file

@ -1,31 +1,30 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* Author : David Beazley
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
****************************************************************************/
static char cvsroot[] = "$Header:";
/* -----------------------------------------------------------------------------
/* -----------------------------------------------------------------------------
* fio.c
*
* This file provides support for formatted I/O via fprint style
* functions.
* This file implements a number of standard I/O operations included
* formatted output, readline, and splitting.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header:";
#include "dohint.h"
#define OBUFLEN 512
static DOH *encodings = 0; /* Encoding hash */
/* -----------------------------------------------------------------------------
* Writen()
*
* Write's N characters of output and retries until all characters are
* written. This is useful should a write operation encounter a spurious signal.
* ----------------------------------------------------------------------------- */
static int Writen(DOH *out, void *buffer, int len) {
int nw = len, ret;
char *cb = (char *) buffer;
@ -39,18 +38,19 @@ static int Writen(DOH *out, void *buffer, int len) {
}
/* -----------------------------------------------------------------------------
* void DohEncoding(char *name, DOH *(*fn)(DOH *s))
* DohEncoding()
*
* Register a printf named encoding method.
* Registers a new printf encoding method. An encoder function should accept
* two file-like objects and operate as a filter.
* ----------------------------------------------------------------------------- */
static DOH *encodings = 0;
void DohEncoding(char *name, DOH *(*fn)(DOH *s))
{
void
DohEncoding(char *name, DOH *(*fn)(DOH *s)) {
if (!encodings) encodings = NewHash();
Setattr(encodings,(void *) name, NewVoid((void *)fn,0));
}
/* internal function for processing an encoding */
static DOH *encode(char *name, DOH *s) {
DOH *handle, *ns;
DOH *(*fn)(DOH *);
@ -67,9 +67,21 @@ static DOH *encode(char *name, DOH *s) {
}
/* -----------------------------------------------------------------------------
* DohvPrintf(DOH *so, char *format, va_list ap)
* DohvPrintf()
*
* printf
* DOH implementation of printf. Output can be directed to any file-like object
* including bare FILE * objects. The same formatting codes as printf are
* recognized with two extensions:
*
* %s - Prints a "char *" or the string representation of any
* DOH object. This will implicitly result in a call to
* Str(obj).
*
* %(encoder)* - Filters the output through an encoding function registered
* with DohEncoder().
*
* Note: This function is not particularly memory efficient with large strings.
* It's better to use Dump() or some other method instead.
* ----------------------------------------------------------------------------- */
int
@ -357,8 +369,14 @@ DohvPrintf(DOH *so, char *format, va_list ap)
return nbytes;
}
/* Printf */
int DohPrintf(DOH *obj, char *format, ...) {
/* -----------------------------------------------------------------------------
* DohPrintf()
*
* Variable length argument entry point to Printf
* ----------------------------------------------------------------------------- */
int
DohPrintf(DOH *obj, char *format, ...) {
va_list ap;
int ret;
va_start(ap,format);
@ -367,9 +385,15 @@ int DohPrintf(DOH *obj, char *format, ...) {
return ret;
}
/* DohCopyto(DOH *in, DOH *out) */
/* -----------------------------------------------------------------------------
* DohCopyto()
*
* Copies all of the input from an input stream to an output stream. Returns the
* number of bytes copied.
* ----------------------------------------------------------------------------- */
int DohCopyto(DOH *in, DOH *out) {
int
DohCopyto(DOH *in, DOH *out) {
int nbytes = 0, ret;
int nwrite = 0, wret;
char *cw;
@ -394,9 +418,16 @@ int DohCopyto(DOH *in, DOH *out) {
}
}
/* Split by a character */
DOH *DohSplit(DOH *in, char *chs, int nsplits) {
/* -----------------------------------------------------------------------------
* DohSplit()
*
* Split an input stream into a list of strings delimeted by characters in a
* string. Optionally accepts a maximum number of splits to perform.
* ----------------------------------------------------------------------------- */
DOH *
DohSplit(DOH *in, char *chs, int nsplits) {
DOH *list;
DOH *str;
int c;
@ -425,9 +456,14 @@ DOH *DohSplit(DOH *in, char *chs, int nsplits) {
return list;
}
/* Read a single line of text */
/* -----------------------------------------------------------------------------
* DohReadline()
*
* Read a single input line and return it as a string.
* ----------------------------------------------------------------------------- */
DOH *DohReadline(DOH *in) {
DOH *
DohReadline(DOH *in) {
char c;
int n = 0;
DOH *s = NewString("");

View file

@ -1,25 +1,15 @@
/****************************************************************************
* DOH
*
* Author : David Beazley
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
****************************************************************************/
static char cvsroot[] = "$Header$";
/***********************************************************************
/* -----------------------------------------------------------------------------
* hash.c
*
* Hash table implementation.
***********************************************************************/
* Implements a simple hash table object.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
#include "dohint.h"
@ -40,12 +30,7 @@ typedef struct Hash {
HashNode *current;
} Hash;
/* -----------------------------------------------------------------------------
Key interning. This is used for getattr,setattr functions.
The following structure maps raw char * entries to string objects.
----------------------------------------------------------------------------- */
/* Key interning structure */
typedef struct KeyValue {
char *cstr;
DOH *sstr;
@ -55,6 +40,7 @@ typedef struct KeyValue {
static KeyValue *root = 0;
/* Find or create a key in the interned key table */
static DOH *find_key(char *c) {
KeyValue *r, *s;
int d = 0;
@ -85,8 +71,8 @@ static DOH *find_key(char *c) {
#define HASH_INIT_SIZE 7
static HashNode *NewNode(DOH *k, void *obj)
{
/* Create a new hash node */
static HashNode *NewNode(DOH *k, void *obj) {
HashNode *hn = (HashNode *) DohMalloc(sizeof(HashNode));
hn->key = k;
Incref(hn->key);
@ -96,22 +82,21 @@ static HashNode *NewNode(DOH *k, void *obj)
return hn;
}
static void DelNode(HashNode *hn)
{
/* Delete a hash node */
static void DelNode(HashNode *hn) {
Delete(hn->key);
Delete(hn->object);
DohFree(hn);
}
int Hash_check(DOH *);
/* -----------------------------------------------------------------------------
* DelHash() - Delete a hash table
* DelHash()
*
* Delete a hash table.
* ----------------------------------------------------------------------------- */
static void
DelHash(DOH *ho)
{
DelHash(DOH *ho) {
Hash *h;
HashNode *n,*next;
int i;
@ -134,12 +119,13 @@ DelHash(DOH *ho)
}
/* -----------------------------------------------------------------------------
* Hash_clear(DOH *ho) - Clear all entries in a hash table
* Hash_clear()
*
* Clear all of the entries in the hash table.
* ----------------------------------------------------------------------------- */
static void
Hash_clear(DOH *ho)
{
Hash_clear(DOH *ho) {
Hash *h;
HashNode *n,*next;
int i;
@ -159,12 +145,13 @@ Hash_clear(DOH *ho)
}
/* -----------------------------------------------------------------------------
* Hash_scope(DOH *ho, int s) - Clear all entries in a hash table
* Hash_scope()
*
* Change the scope of the hash table.
* ----------------------------------------------------------------------------- */
static void
Hash_scope(DOH *ho, int s)
{
Hash_scope(DOH *ho, int s) {
Hash *h;
HashNode *n;
int i;
@ -184,10 +171,7 @@ Hash_scope(DOH *ho, int s)
h->flags = h->flags & ~DOH_FLAG_SETSCOPE;
}
/* -----------------------------------------------------------------------------
* static resize(Hash *h) - Resizes the hash table
* ----------------------------------------------------------------------------- */
/* resize the hash table */
static void resize(Hash *h) {
HashNode *n, *next, **table;
int oldsize, newsize;
@ -233,7 +217,10 @@ static void resize(Hash *h) {
}
/* -----------------------------------------------------------------------------
* int Hash_setattr(DOH *h, DOH *k, DOH *obj) - Adds an object to a hash
* Hash_setattr()
*
* Set an attribute in the hash table. Deletes the existing entry if it already
* exists.
* ----------------------------------------------------------------------------- */
static int
@ -280,7 +267,9 @@ Hash_setattr(DOH *ho, DOH *k, DOH *obj) {
}
/* -----------------------------------------------------------------------------
* DOH *Hash_getattr(DOH *ho, DOH *k) - Get an item from the hash table
* Hash_getattr()
*
* Get an attribute from the hash table. Returns 0 if it doesn't exist.
* ----------------------------------------------------------------------------- */
static DOH *
@ -301,12 +290,13 @@ Hash_getattr(DOH *ho, DOH *k) {
}
/* -----------------------------------------------------------------------------
* void Hash_delattr(DOH *ho, DOH *k) - Delete an element from the table
* Hash_delattr()
*
* Delete an object from the hash table.
* ----------------------------------------------------------------------------- */
static int
Hash_delattr(DOH *ho, DOH *k)
{
Hash_delattr(DOH *ho, DOH *k) {
HashNode *n, *prev;
int hv;
Hash *h;
@ -334,10 +324,7 @@ Hash_delattr(DOH *ho, DOH *k)
return 0;
}
/* -----------------------------------------------------------------------------
* Iterators
* ----------------------------------------------------------------------------- */
/* General purpose iterators */
static HashNode *
hash_first(DOH *ho) {
Hash *h = (Hash *) ho;
@ -367,41 +354,39 @@ hash_next(DOH *ho) {
h->current = h->hashtable[h->currentindex];
return h->current;
}
static DOH *
Hash_first(DOH *ho) {
HashNode *hn = hash_first(ho);
if (hn) return hn->object;
return 0;
}
/* -----------------------------------------------------------------------------
* Hash_firstkey()
*
* Return first hash-table key.
* ----------------------------------------------------------------------------- */
static DOH *
Hash_next(DOH *ho)
{
HashNode *hn = hash_next(ho);
if (hn) return hn->object;
return 0;
}
static DOH *
Hash_firstkey(DOH *ho)
{
Hash_firstkey(DOH *ho) {
HashNode *hn = hash_first(ho);
if (hn) return hn->key;
return 0;
}
/* -----------------------------------------------------------------------------
* Hash_nextkey()
*
* Return next hash table key.
* ----------------------------------------------------------------------------- */
static DOH *
Hash_nextkey(DOH *ho)
{
Hash_nextkey(DOH *ho) {
HashNode *hn = hash_next(ho);
if (hn) return hn->key;
return 0;
}
/* -----------------------------------------------------------------------------
* String *Hash_str(DOH *ho) - Create a string representation of a hash
* Hash_str()
*
* Create a string representation of a hash table (mainly for debugging).
* ----------------------------------------------------------------------------- */
static DOH *
Hash_str(DOH *ho) {
int i;
@ -430,7 +415,9 @@ Hash_str(DOH *ho) {
}
/* -----------------------------------------------------------------------------
* Hash_len(DOH *)
* Hash_len()
*
* Return number of entries in the hash table.
* ----------------------------------------------------------------------------- */
static int
@ -444,7 +431,6 @@ Hash_len(DOH *ho) {
*
* Return a list of keys
* ----------------------------------------------------------------------------- */
DOH *
Hash_keys(DOH *so) {
DOH *keys;
@ -461,7 +447,9 @@ Hash_keys(DOH *so) {
}
/* -----------------------------------------------------------------------------
* DOH *CopyHash(DOH *ho) - Copy a hash table
* CopyHash()
*
* Make a copy of a hash table. Note: this is a shallow copy.
* ----------------------------------------------------------------------------- */
static DOH *
@ -471,6 +459,7 @@ CopyHash(DOH *ho) {
int i;
h = (Hash *) ho;
nh = (Hash *) DohObjMalloc(sizeof(Hash));
nh->objinfo = h->objinfo;
DohXInit(h);
nh->hashsize = h->hashsize;
nh->hashtable = (HashNode **) DohMalloc(nh->hashsize*sizeof(HashNode *));
@ -480,7 +469,6 @@ CopyHash(DOH *ho) {
nh->currentindex = -1;
nh->current = 0;
nh->nitems = 0;
nh->objinfo = h->objinfo;
nh->line = h->line;
nh->file = h->file;
if (nh->file) Incref(nh->file);
@ -508,13 +496,6 @@ static DohMappingMethods HashMappingMethods = {
Hash_nextkey,
};
static DohPositionalMethods HashPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohObjInfo HashType = {
"Hash", /* objname */
sizeof(Hash), /* size */
@ -534,10 +515,17 @@ static DohObjInfo HashType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
&HashPositionalMethods, /* doh_positional */
0, /* doh_positional */
};
int Hash_check(DOH *so) {
/* -----------------------------------------------------------------------------
* Hash_check()
*
* Return 1 if an object is a hash table object.
* ----------------------------------------------------------------------------- */
int
Hash_check(DOH *so) {
Hash *h = (Hash *) so;
if (!h) return 0;
if (!DohCheck(so)) return 0;
@ -546,13 +534,17 @@ int Hash_check(DOH *so) {
}
/* -----------------------------------------------------------------------------
* NewHash() - Create a new hash table
* NewHash()
*
* Create a new hash table.
* ----------------------------------------------------------------------------- */
DOH *NewHash() {
DOH *
NewHash() {
Hash *h;
int i;
h = (Hash *) DohObjMalloc(sizeof(Hash));
h->objinfo = &HashType;
DohXInit(h);
h->hashsize = HASH_INIT_SIZE;
h->hashtable = (HashNode **) DohMalloc(h->hashsize*sizeof(HashNode *));
@ -562,8 +554,9 @@ DOH *NewHash() {
h->currentindex = -1;
h->current = 0;
h->nitems = 0;
h->objinfo = &HashType;
return (DOH *) h;
}

View file

@ -1,26 +1,16 @@
/****************************************************************************
* DOH
/* -----------------------------------------------------------------------------
* list.c
*
* Implements a simple list object.
*
* Author : David Beazley
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
****************************************************************************/
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
/*******************************************************************************
* File : list.c
*
* General purpose structure for keeping a list of reference counted Swig objects.
*******************************************************************************/
#include "dohint.h"
typedef struct List {
@ -31,9 +21,7 @@ typedef struct List {
DOH **items;
} List;
int List_check(DOH *);
/* Internal function. Doubles amount of memory in a list */
/* Doubles amount of memory in a list */
static
void more(List *l) {
int i;
@ -52,7 +40,9 @@ void more(List *l) {
}
/* -----------------------------------------------------------------------------
* DOH *CopyList(DOH *l) - Copy a list
* CopyList()
*
* Make a shallow copy of a list.
* ----------------------------------------------------------------------------- */
static DOH *
@ -61,8 +51,8 @@ CopyList(DOH *lo) {
int i;
l = (List *) lo;
nl = (List *) DohObjMalloc(sizeof(List));
DohXInit(nl);
nl->objinfo = l->objinfo;
DohXInit(nl);
nl->nitems = l->nitems;
nl->maxitems = l->maxitems;
nl->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
@ -80,7 +70,9 @@ CopyList(DOH *lo) {
}
/* -----------------------------------------------------------------------------
* void DelList(DOH *l) - Delete a list
* DelList()
*
* Delete a list.
* ----------------------------------------------------------------------------- */
static void
@ -99,7 +91,9 @@ DelList(DOH *lo) {
}
/* -----------------------------------------------------------------------------
* void List_clear(DOH *l) - Clear all elements in the list
* List_clear()
*
* Remove all of the list entries, but keep the list object intact.
* ----------------------------------------------------------------------------- */
static void
@ -115,8 +109,11 @@ List_clear(DOH *lo) {
}
/* -----------------------------------------------------------------------------
* void List_scope(DOH *lo, int s)
* List_scope()
*
* Change the scope setting of the list.
* ----------------------------------------------------------------------------- */
static void
List_scope(DOH *lo, int s) {
List *l;
@ -132,18 +129,19 @@ List_scope(DOH *lo, int s) {
}
/* -----------------------------------------------------------------------------
* void List_insert(DOH *lo, int pos, DOH *item) - Insert an element
* List_insert()
*
* Insert an item into the list. If the item is not a DOH object, it is assumed
* to be a 'char *' and is used to construct an equivalent string object.
* ----------------------------------------------------------------------------- */
static int
List_insert(DOH *lo, int pos, DOH *item)
{
List_insert(DOH *lo, int pos, DOH *item) {
List *l;
DohBase *b;
int i, no = 0;
if (!item) return -1;
l = (List *) lo;
if (!DohCheck(item)) {
@ -168,12 +166,13 @@ List_insert(DOH *lo, int pos, DOH *item)
}
/* -----------------------------------------------------------------------------
* void List_remove(DOH *lo, int pos) - Remove an element
* List_remove()
*
* Remove an item from a list.
* ----------------------------------------------------------------------------- */
static int
List_remove(DOH *lo, int pos)
{
List_remove(DOH *lo, int pos) {
List *l;
DOH *item;
int i;
@ -192,22 +191,24 @@ List_remove(DOH *lo, int pos)
}
/* -----------------------------------------------------------------------------
* int List_len(DOH *l) - List length
* List_len()
*
* Return the number of elements in the list
* ----------------------------------------------------------------------------- */
static int
List_len(DOH *lo)
{
List_len(DOH *lo) {
return ((List *) lo)->nitems;
}
/* -----------------------------------------------------------------------------
* DOH *List_get(DOH *lo, int n) - Get an item
* List_get()
*
* Get the nth item from the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_get(DOH *lo, int n)
{
List_get(DOH *lo, int n) {
List *l;
l = (List *) lo;
if (n == DOH_END) n = l->nitems-1;
@ -219,12 +220,13 @@ List_get(DOH *lo, int n)
}
/* -----------------------------------------------------------------------------
* int List_set(DOH *lo, int n, DOH *val) - Set an item
* List_set()
*
* Set the nth item in the list replacing any previous item.
* ----------------------------------------------------------------------------- */
static int
List_set(DOH *lo, int n, DOH *val)
{
List_set(DOH *lo, int n, DOH *val) {
List *l;
int no = 0;
l = (List *) lo;
@ -246,12 +248,13 @@ List_set(DOH *lo, int n, DOH *val)
}
/* -----------------------------------------------------------------------------
* Iterators
* List_first()
*
* Return the first item in the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_first(DOH *lo)
{
List_first(DOH *lo) {
List *l;
l = (List *) lo;
l->iter = 0;
@ -259,9 +262,14 @@ List_first(DOH *lo)
return l->items[l->iter];
}
/* -----------------------------------------------------------------------------
* List_next()
*
* Return the next item in the list.
* ----------------------------------------------------------------------------- */
static DOH *
List_next(DOH *lo)
{
List_next(DOH *lo) {
List *l;
l = (List *) lo;
l->iter++;
@ -270,12 +278,12 @@ List_next(DOH *lo)
}
/* -----------------------------------------------------------------------------
* String *List_str() - Create a string representation
* List_str()
*
* Create a string representation of the list
* ----------------------------------------------------------------------------- */
static DOH *
List_str(DOH *lo)
{
List_str(DOH *lo) {
DOH *s;
int i;
@ -297,6 +305,12 @@ List_str(DOH *lo)
return s;
}
/* -----------------------------------------------------------------------------
* List_dump()
*
* Dump the items to an output stream.
* ----------------------------------------------------------------------------- */
static int
List_dump(DOH *lo, DOH *out) {
int nsent = 0;
@ -310,26 +324,6 @@ List_dump(DOH *lo, DOH *out) {
return nsent;
}
/* -----------------------------------------------------------------------------
* void List_sort(DOH *DOH)
*
* Sorts a list
* ----------------------------------------------------------------------------- */
static int objcmp(const void *s1, const void *s2) {
DOH **so1, **so2;
so1 = (DOH **) s1;
so2 = (DOH **) s2;
return Cmp(*so1,*so2);
}
void List_sort(DOH *so) {
List *l;
if (!List_check(so)) return;
l = (List *) so;
qsort(l->items,l->nitems,sizeof(DOH *), objcmp);
}
#define MAXLISTITEMS 8
static DohSequenceMethods ListSeqMethods = {
@ -341,13 +335,6 @@ static DohSequenceMethods ListSeqMethods = {
List_next,
};
static DohPositionalMethods ListPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohObjInfo ListType = {
"List", /* objname */
sizeof(List), /* List size */
@ -367,13 +354,14 @@ static DohObjInfo ListType = {
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
&ListPositionalMethods, /* doh_position */
0, /* doh_position */
};
/* -----------------------------------------------------------------------------
* List_check(DOH *lo) - Check to see if an object is a list
* List_check()
*
* Return 1 if an object is a List object.
* ----------------------------------------------------------------------------- */
int
List_check(DOH *lo) {
List *l = (List *) lo;
@ -384,7 +372,9 @@ List_check(DOH *lo) {
}
/* -----------------------------------------------------------------------------
* NewList() - Create a new list
* NewList()
*
* Create a new list.
* ----------------------------------------------------------------------------- */
DOH *
@ -392,8 +382,8 @@ NewList() {
List *l;
int i;
l = (List *) DohObjMalloc(sizeof(List));
DohXInit(l);
l->objinfo = &ListType;
DohXInit(l);
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
@ -403,3 +393,24 @@ NewList() {
l->iter = 0;
return (DOH *) l;
}
/* -----------------------------------------------------------------------------
* List_sort()
*
* Sorts a list
* ----------------------------------------------------------------------------- */
static int objcmp(const void *s1, const void *s2) {
DOH **so1, **so2;
so1 = (DOH **) s1;
so2 = (DOH **) s2;
return Cmp(*so1,*so2);
}
void
List_sort(DOH *so) {
List *l;
if (!List_check(so)) return;
l = (List *) so;
qsort(l->items,l->nitems,sizeof(DOH *), objcmp);
}

View file

@ -1,17 +1,14 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
/* -----------------------------------------------------------------------------
* memory.c
*
* This file implements all of DOH's memory management including allocation
* of objects, checking of objects, and garbage collection.
*
* Author : David Beazley
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
****************************************************************************/
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
@ -33,14 +30,7 @@ static int _DohMemoryCurrent = 0;
static int _DohMemoryHigh = 0;
static int _PoolSize = DOH_POOL_SIZE;
DOH *DohNone = 0;
/* -----------------------------------------------------------------------------
* memory.c
*
* Doh memory manager. Keeps pools of memory around for allocating objects.
* Pools are used to determine if an object is really a Doh object or not.
* ----------------------------------------------------------------------------- */
DOH *DohNone = 0; /* The DOH None object */
typedef struct fragment {
char *ptr; /* Pointer to fragment */
@ -64,13 +54,13 @@ static int nscopes = 0;
/* ----------------------------------------------------------------------
* Pool *CreatePool(int size)
* CreatePool()
*
* Create a new pool
* Create a new memory pool
* ---------------------------------------------------------------------- */
Pool *CreatePool(int size)
{
static Pool *
CreatePool(int size) {
Pool *p = 0;
char *c;
c = (char *) DohMalloc(size);
@ -85,12 +75,13 @@ Pool *CreatePool(int size)
}
/* ----------------------------------------------------------------------
* void InitPools()
* InitPools()
*
* Initialize the memory allocator
* ---------------------------------------------------------------------- */
static void InitPools() {
static void
InitPools() {
int i;
if (pools_initialized) return;
for (i = 0; i < DOH_MAX_FRAG; i++) {
@ -107,12 +98,14 @@ static void InitPools() {
}
/* ----------------------------------------------------------------------
* int DohCheck(DOH *ptr)
* DohCheck()
*
* Check if ptr is an object we created.
* Returns 1 if an arbitrary pointer is a DOH object. This determination
* is made according to the pointer value only.
* ---------------------------------------------------------------------- */
int DohCheck(DOH *ptr) {
int
DohCheck(DOH *ptr) {
Pool *p = Pools;
char *cptr = (char *) ptr;
while (p) {
@ -123,12 +116,14 @@ int DohCheck(DOH *ptr) {
}
/* ----------------------------------------------------------------------
* int DohObjFreeCheck(DOH *ptr)
* DohObjFreeCheck()
*
* Check if ptr is an object that has been deleted.
* Checks to see if an object was already deleted. Useful when tracking
* down nasty double-free problems.
* ---------------------------------------------------------------------- */
int DohObjFreeCheck(DOH *ptr) {
int
DohObjFreeCheck(DOH *ptr) {
int i;
Fragment *f;
char *cptr = (char *) ptr;
@ -143,12 +138,13 @@ int DohObjFreeCheck(DOH *ptr) {
}
/* ----------------------------------------------------------------------
* int DohNewScope()
* DohNewScope()
*
* Create a new scope in which objects will be placed
* Create a new scope in which objects will be placed. Returns a scope
* identifier.
* ---------------------------------------------------------------------- */
int DohNewScope()
{
int
DohNewScope() {
assert(nscopes < DOH_MAX_SCOPES);
if (!pools_initialized) InitPools();
scopes[nscopes] = 0;
@ -157,11 +153,13 @@ int DohNewScope()
}
/* ----------------------------------------------------------------------
* void DohDelScope(int s) - Delete scope s
* DohDelScope()
*
* Deletes a scope.
* ---------------------------------------------------------------------- */
void DohDelScope(int s)
{
void
DohDelScope(int s) {
int ns;
DohBase *b, *b1;
ns = s;
@ -193,9 +191,14 @@ void DohDelScope(int s)
}
/* ----------------------------------------------------------------------
* void real_objfree(DOH *ptr) - Free a DOH object for real.
* real_objfree()
*
* This is the function that actually frees an object. Invoked by the
* garbage collector.
* ---------------------------------------------------------------------- */
static void real_objfree(DOH *ptr) {
static void
real_objfree(DOH *ptr) {
DohBase *b;
Fragment *f;
b = (DohBase *) ptr;
@ -213,7 +216,7 @@ static void real_objfree(DOH *ptr) {
}
/* ----------------------------------------------------------------------
* void DohGarbageCollect()
* DohGarbageCollect()
*
* This walks through all of the scopes and does garbage collection.
*
@ -259,12 +262,13 @@ DohGarbageCollect() {
}
/* ----------------------------------------------------------------------
* void *DohObjMalloc(size_t size)
* DohObjMalloc()
*
* Allocate an object
* Allocate memory for a new object.
* ---------------------------------------------------------------------- */
void *DohObjMalloc(size_t size) {
void *
DohObjMalloc(size_t size) {
Pool *p;
Fragment *f;
void *ptr = 0;
@ -329,14 +333,14 @@ void *DohObjMalloc(size_t size) {
}
/* ----------------------------------------------------------------------
* void DohObjFree(DOH *ptr)
* DohObjFree()
*
* Free a DOH object. This function doesn't do much of anything with GC.
* Frees a DOH object. Doesn't do much with GC.
* ---------------------------------------------------------------------- */
void DohObjFree(DOH *ptr) {
void
DohObjFree(DOH *ptr) {
DohBase *b;
if (!DohCheck(ptr)) {
DohTrace(DOH_MEMORY,"DohObjFree. %x not a DOH object!\n", ptr);
return; /* Oh well. Guess we're leaky */
@ -349,71 +353,46 @@ void DohObjFree(DOH *ptr) {
}
/* -----------------------------------------------------------------------------
* void *DohMalloc(size_t nbytes)
* DohMalloc()
*
* Wrapper around malloc() function. Records memory usage.
* ----------------------------------------------------------------------------- */
void *DohMalloc(size_t nbytes) {
char *ptr;
ptr = (char *) malloc(nbytes+8);
if (!ptr) return 0;
_DohMemoryCurrent += (nbytes+8);
if (_DohMemoryCurrent > _DohMemoryHigh) _DohMemoryHigh = _DohMemoryCurrent;
*((int *) ptr) = nbytes;
return (void *) (ptr+8);
void *
DohMalloc(size_t nbytes) {
return (void *) malloc(nbytes);
}
/* -----------------------------------------------------------------------------
* void *DohRealloc(void *ptr, size_t newsize)
* DohRealloc()
*
* Wrapper around realloc() function.
* ----------------------------------------------------------------------------- */
void *DohRealloc(void *ptr, size_t newsize) {
char *cptr = (char *) ptr;
char *nptr;
int size;
size = *((int *) (cptr - 8));
nptr = (char *) realloc(cptr-8,newsize+8);
if (!nptr) return 0;
*((int *) nptr) = newsize;
_DohMemoryCurrent += (newsize - size);
if (_DohMemoryCurrent > _DohMemoryHigh) _DohMemoryHigh = _DohMemoryCurrent;
return (void *) (nptr+8);
void *
DohRealloc(void *ptr, size_t newsize) {
return (void *) realloc(ptr,newsize);
}
/* -----------------------------------------------------------------------------
* void DohFree(void *ptr)
* DohFree()
*
* Wrapper around free() function. Records memory usage.
* Wrapper around free() function.
* ----------------------------------------------------------------------------- */
void DohFree(void *ptr) {
char *cptr = (char *) ptr;
int size;
if (!ptr) return;
size = *((int *) (cptr - 8));
free(cptr-8);
_DohMemoryCurrent = _DohMemoryCurrent - (size+8);
void
DohFree(void *ptr) {
free(ptr);
}
/* -----------------------------------------------------------------------------
* int DohMemoryUse()
* DohPoolSize()
*
* Return bytes currently in use by Doh
* Change the size of the memory pools used by DOH
* ----------------------------------------------------------------------------- */
int DohMemoryUse() {
return _DohMemoryCurrent;
}
int DohMemoryHigh() {
return _DohMemoryHigh;
}
int DohPoolSize(int poolsize) {
int
DohPoolSize(int poolsize) {
int ps;
ps = _PoolSize;
if (poolsize > 0) {

View file

@ -1,25 +1,16 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* Author : David Beazley
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
****************************************************************************/
static char cvsroot[] = "$Header$";
/* ---------------------------------------------------------------------------
/* -----------------------------------------------------------------------------
* string.c
*
* String object.
* --------------------------------------------------------------------------- */
* Implements a string object that supports both sequence operations and
* file semantics.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
#include "dohint.h"
@ -69,13 +60,6 @@ static DohSequenceMethods StringSeqMethods = {
0, /* doh_next */
};
static DohPositionalMethods StringPositionalMethods = {
XBase_setfile,
XBase_getfile,
XBase_setline,
XBase_getline
};
static DohFileMethods StringFileMethods = {
String_read,
String_write,
@ -111,7 +95,7 @@ static DohObjInfo StringType = {
&StringFileMethods,/* doh_file */
&StringStringMethods, /* doh_string */
0, /* doh_callable */
&StringPositionalMethods, /* doh_position */
0, /* doh_position */
};
#define INIT_MAXSIZE 16
@ -160,6 +144,7 @@ NewString(char *s)
String *str;
str = (String *) DohObjMalloc(sizeof(String));
str->objinfo = &StringType;
DohXInit(str);
str->hashkey = -1;
str->sp = 0;
str->lsp = 0;

View file

@ -1,27 +1,17 @@
/****************************************************************************
* DOH (Dynamic Object Hack)
*
* Author : David Beazley
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which SWIG
* can be used and distributed.
****************************************************************************/
static char cvsroot[] = "$Header$";
/* -----------------------------------------------------------------------------
/* -----------------------------------------------------------------------------
* void.c
*
* Void Object
* Implements a "void" object that is really just a DOH container around
* an arbitrary C/C++ object represented as a void *.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
* ----------------------------------------------------------------------------- */
static char cvsroot[] = "$Header$";
#include "dohint.h"
typedef struct {
@ -30,9 +20,46 @@ typedef struct {
void (*del)(void *);
} VoidObj;
void Void_delete(DOH *);
DOH *Void_copy(DOH *);
void *Void_data(DOH *);
/* -----------------------------------------------------------------------------
* Void_delete()
*
* Delete a void object. Invokes the destructor supplied at the time of creation.
* ----------------------------------------------------------------------------- */
static void
Void_delete(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
if (v->del) {
(*v->del)(v->ptr);
}
v->del = 0;
DohObjFree(v);
}
/* -----------------------------------------------------------------------------
* Void_copy()
*
* Copies a void object. This is only a shallow copy. The object destruction
* function is not copied in order to avoid potential double-free problems.
* ----------------------------------------------------------------------------- */
static DOH *
Void_copy(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
return NewVoid(v->ptr,0);
}
/* -----------------------------------------------------------------------------
* Void_data()
*
* Returns the void * stored in the object.
* ----------------------------------------------------------------------------- */
static void *
Void_data(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
return v->ptr;
}
static DohObjInfo DohVoidType = {
"VoidObj", /* objname */
@ -63,7 +90,15 @@ static DohObjInfo DohVoidType = {
};
DOH *NewVoid(void *obj, void (*del)(void *)) {
/* -----------------------------------------------------------------------------
* NewVoid()
*
* Creates a new Void object given a void * and a pointer to an optional
* destruction function.
* ----------------------------------------------------------------------------- */
DOH *
NewVoid(void *obj, void (*del)(void *)) {
VoidObj *v;
v = (VoidObj *) DohObjMalloc(sizeof(VoidObj));
v->objinfo = &DohVoidType;
@ -71,23 +106,3 @@ DOH *NewVoid(void *obj, void (*del)(void *)) {
v->del = del;
return (DOH *) v;
}
void Void_delete(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
if (v->del) {
(*v->del)(v->ptr);
}
v->del = 0;
DohObjFree(v);
}
DOH *Void_copy(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
return NewVoid(v->ptr,0);
}
void *Void_data(DOH *vo) {
VoidObj *v = (VoidObj *) vo;
return v->ptr;
}

View file

@ -1,26 +1,15 @@
/*******************************************************************************
* DOH (Dave's Object Hack)
*
* Author : David Beazley
*
* Department of Computer Science
* University of Chicago
* 1100 E 58th Street
* Chicago, IL 60637
* beazley@cs.uchicago.edu
*
* Please read the file LICENSE for the copyright and terms by which DOH
* can be used and distributed.
*******************************************************************************/
/***********************************************************************
* $Header$
*
/* -----------------------------------------------------------------------------
* doh.h
*
* DOH is really not much more than an interface. This file describes
* the interface.
***********************************************************************/
* This file describes of the externally visible functions in DOH.
*
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
*
* Copyright (C) 1999-2000. The University of Chicago
* See the file LICENSE for information on usage and redistribution.
*
* $Header$
* ----------------------------------------------------------------------------- */
#ifndef _DOH_H
#define _DOH_H
@ -151,8 +140,6 @@ typedef struct DohObjInfo {
extern void DohInit(DOH *obj); /* Initialize an object */
extern void DohXInit(DOH *obj); /* Initialize extended object */
extern int DohCheck(DOH *ptr); /* Check if a DOH object */
extern int DohMemoryUse(); /* Return current memory */
extern int DohMemoryHigh(); /* High memory use */
extern int DohPoolSize(int); /* Set memory alloc size */
extern int DohNewScope(); /* Create a new scope */
extern void DohDelScope(int); /* Delete a scope */

View file

@ -6,8 +6,3 @@
#include <stdarg.h>
#include "doh.h"
extern void XBase_setfile(DOH *, DOH *);
extern DOH *XBase_getfile(DOH *);
extern void XBase_setline(DOH *, int);
extern int XBase_getline(DOH *);