diff options
-rw-r--r-- | zwgc/Dictionary/Imakefile | 53 | ||||
-rw-r--r-- | zwgc/Memory/Imakefile | 33 | ||||
-rw-r--r-- | zwgc/Memory/new_memory.c | 252 | ||||
-rw-r--r-- | zwgc/Memory/new_memory.h | 86 | ||||
-rw-r--r-- | zwgc/String/Imakefile | 35 | ||||
-rw-r--r-- | zwgc/String/new_string.c | 197 | ||||
-rw-r--r-- | zwgc/String/new_string.h | 136 |
7 files changed, 792 insertions, 0 deletions
diff --git a/zwgc/Dictionary/Imakefile b/zwgc/Dictionary/Imakefile new file mode 100644 index 0000000..7142280 --- /dev/null +++ b/zwgc/Dictionary/Imakefile @@ -0,0 +1,53 @@ +/**/# Copyright 1988 Massachusetts Institute of Technology. +/**/# +/**/# For copying and distribution information, see the file +/**/# "mit-copyright.h". +/**/# +/**/# $Source$ +/**/# $Author$ +/**/# $Header$ +/**/# + +LIBS= $(ZEPHYR_LIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) +LINTLIBS=$(ZEPHYR_LINTLIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) + +OBJS = string_dictionary_aux.o int_dictionary.o string_dictionary.o + +REALSRCS = dictionary.c string_dictionary_aux.c +REALHDRS = dictionary.h string_dictionary_aux.h +SRCS = string_dictionary_aux.c int_dictionary.c string_dictionary.c +HDRS = string_dictionary_aux.h int_dictionary.h string_dictionary.h + +LINCLUDES = -I../Memory -I../String + +GENERATORS= generate_dictionary_instance + +SRCDIR= ${SRCTOP}/zwgc/zwgc.dev/Dictionary +CODE= ${GENERATORS} ${REALSRCS} ${REALHDRS} Imakefile + +normal_obj_rule() + +all:: dictionary_spec $(OBJS) + +depend:: ${GENERATORS} ${SRCS} + +# +# /* Automatically generate a spec file from the template include file: */ +# +dictionary_spec: dictionary.h + cat dictionary.h | grep "^.\*" | grep -v "^ \*/" | sed 's/.\*//' > dictionary_spec + +# +# Generate a int dictionary instance: +# +int_dictionary.c int_dictionary.h: dictionary.c dictionary.h + ./generate_dictionary_instance int +# +# Generate a string dictionary instance: +# +string_dictionary.c string_dictionary.h: dictionary.c dictionary.h + ./generate_dictionary_instance string new_string.h + +clean:: + $(RM) $(OBJS) dictionary_spec int_dictionary.c string_dictionary.c \ + int_dictionary.h string_dictionary.h diff --git a/zwgc/Memory/Imakefile b/zwgc/Memory/Imakefile new file mode 100644 index 0000000..8fc0e0c --- /dev/null +++ b/zwgc/Memory/Imakefile @@ -0,0 +1,33 @@ +/**/# Copyright 1988 Massachusetts Institute of Technology. +/**/# +/**/# For copying and distribution information, see the file +/**/# "mit-copyright.h". +/**/# +/**/# $Source$ +/**/# $Author$ +/**/# $Header$ +/**/# + +LIBS= $(ZEPHYR_LIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) +LINTLIBS=$(ZEPHYR_LINTLIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) + +OBJS = new_memory.o +SRCS = new_memory.c +HDRS = new_memory.h + +SRCDIR= ${SRCTOP}/zwgc/zwgc.dev/Memory +CODE= ${SRCS} ${HDRS} Imakefile + +normal_obj_rule() + +all:: memory_spec $(OBJS) + +# +# /* Automatically generate a spec file from the template include file: */ +# +memory_spec: new_memory.h + cat new_memory.h | grep "^.\*" | grep -v "^ \*/" | sed 's/.\*//' > memory_spec + +clean:: + $(RM) $(OBJS) memory_spec + diff --git a/zwgc/Memory/new_memory.c b/zwgc/Memory/new_memory.c new file mode 100644 index 0000000..eb839dd --- /dev/null +++ b/zwgc/Memory/new_memory.c @@ -0,0 +1,252 @@ +/* This file is part of the Project Athena Zephyr Notification System. + * It is one of the source files comprising zwgc, the Zephyr WindowGram + * client. + * + * Created by: Marc Horowitz <marc@athena.mit.edu> + * + * $Source$ + * $Author$ + * + * Copyright (c) 1989 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file + * "mit-copyright.h". + */ + +#if (!defined(lint) && !defined(SABER)) +static char rcsid_new_memory_c[] = "$Id$"; +#endif + +/* This entire module goes out the window in saber */ +#ifndef SABER +/* + * memory - module wrapping debugging code around normal malloc/free/etc. + * routines. + * + * Overview: + * + * ... + */ + +#define memory__PROVIDER +#include "new_memory.h" + +/* + * + */ +extern char *malloc(); +extern char *realloc(); +char *calloc(); +extern int free(); + +/* + * + */ +#ifdef DEBUG +#define assert(x) if (!(x)) abort() +#else +#define assert(x) +#endif + +/* + * + */ +#ifdef DEBUG_MEMORY + +#include <stdio.h> + +extern void record_request(); +char *current_module = 0; +int current_line = -1; + +#endif + +/* + * string string_CreateFromData(char *data, int length): + * Requires: data[0], data[1], ..., data[length-1] != 0 + * Effects: Takes the first length characters at data and + * creates a string containing them. The returned string + * is on the heap & must be freed eventually. + * I.e., if passed "foobar" and 3, it would return + * string_Copy("foo"). + */ + +char *memory__malloc(size) + unsigned size; +{ + char *result; + + result = malloc(size + memory__size_of_header); + if (!result) + abort(); /* <<<>>> */ + +#ifdef DEBUG_MEMORY + ((memory_block_header *)result)->size = size; + ((memory_block_header *)result)->creating_module = current_module; + ((memory_block_header *)result)->line_number_in_creating_module = + current_line; + ((memory_block_header *)result)->check_field = CHECK_FIELD_VALUE; + result += memory__size_of_header; + + record_request(current_module, current_line, 1, size); +#endif + + return(result); +} + +char *memory__realloc(ptr, size) + char *ptr; + unsigned size; +{ + char *result; + + assert(ptr); + +#ifdef DEBUG_MEMORY + if (!memory__on_heap_p(ptr)) { + printf("realloced non-memory block in %s on line %d!\n", + current_module, current_line); + fflush(stdout); + return(realloc(ptr, size)); + } +#endif + + result = realloc(ptr-memory__size_of_header, size+memory__size_of_header); + if (!result) + abort(); /* <<<>>> */ + + return(result+memory__size_of_header); +} + +char *memory__calloc(nelem, elsize) + unsigned nelem; + unsigned elsize; +{ + char *result; + +#ifdef DEBUG_MEMORY + printf("in calloc\n"); fflush(stdout); +#endif + + abort(); + +#ifdef FRED + result = calloc(nelem, elsize); + if (!result) + abort(); + + record_request(1); +#endif + + return(result); +} + +void memory__free(ptr) + char *ptr; +{ + assert(ptr); + +#ifdef DEBUG_MEMORY + if (!memory__on_heap_p(ptr)) { + printf("freed non-memory block in %s on line %d!\n", current_module, + current_line); + fflush(stdout); + (void)free(ptr); + return; + } + + record_request(memory__get_header(ptr)->creating_module, + memory__get_header(ptr)->line_number_in_creating_module, + -1, + memory__get_header(ptr)->size); +#endif + + (void)free(ptr-memory__size_of_header); +} + +#ifdef DEBUG_MEMORY + +#include "../Dictionary/int_dictionary.h" + +static int request_off = 0; +static int_dictionary requests = 0; +static int outstanding_requests = 0; +static int outstanding_memory = 0; + +void record_request(module, line_number, dir, size) + char *module; + int line_number; + int dir; + unsigned int size; +{ + int_dictionary_binding *binding; + int already_exists; +#ifdef LINE + char buffer[20]; +#endif + + if (request_off) + return; + request_off = 1; + + if (!requests) + requests = int_dictionary_Create(101); + +#ifdef LINE + module = string_Concat(module, ":"); + sprintf(buffer, "%d", line_number); + module = string_Concat2(module, buffer); +#endif + + binding = int_dictionary_Define(requests, module, &already_exists); + if (!already_exists) + binding->value = 0; + +#ifdef LINE + free(module); +#endif + + binding->value += dir; + outstanding_requests += dir; + outstanding_memory += size*dir; + + request_off = 0; +} + +void proc(binding) + int_dictionary_binding *binding; +{ + if (binding->value) + printf(" %-30s %6d blocks allocated\n", binding->key, binding->value); +} + +void report_memory_usage() +{ + printf("\n# of blocks on the heap = %d\n", outstanding_requests); + printf("Total heap space in use: %d bytes\n", outstanding_memory); + + printf("\nHeap Allocations by module:\n"); + int_dictionary_Enumerate(requests, proc); + printf("\n"); + + fflush(stdout); +} + +void set_module(file, line) + char *file; + int line; +{ + if (request_off) + return; + + if (!strcmp(file, "new_string.c")) + return; + if (!strcmp(file, "string_dictionary_aux.c")) + return; + + current_line = line; + current_module = file; +} + +#endif + +#endif /* SABER */ diff --git a/zwgc/Memory/new_memory.h b/zwgc/Memory/new_memory.h new file mode 100644 index 0000000..5294338 --- /dev/null +++ b/zwgc/Memory/new_memory.h @@ -0,0 +1,86 @@ +/* This file is part of the Project Athena Zephyr Notification System. + * It is one of the source files comprising zwgc, the Zephyr WindowGram + * client. + * + * Created by: Marc Horowitz <marc@athena.mit.edu> + * + * $Source$ + * $Author$ + * $Id$ + * + * Copyright (c) 1989 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file + * "mit-copyright.h". + */ + +/* This entire module goes out the window in saber */ +#ifndef SABER + +#ifndef memory_MODULE +#define memory_MODULE + +extern char *memory__malloc(); /* PRIVATE */ +extern char *memory__realloc(); /* PRIVATE */ +extern char *memory__calloc(); /* PRIVATE */ +extern void memory__free(); /* PRIVATE */ + +#ifdef DEBUG_MEMORY + +#define CHECK_FIELD_VALUE 0xe5e7e3e9 + +typedef struct _memory_block_header { + unsigned size; + char *creating_module; + int line_number_in_creating_module; + unsigned int check_field; +} memory_block_header; + +#define memory__size_of_header (sizeof(struct _memory_block_header)) + +#define memory__get_header(block) \ + ((struct _memory_block_header *)((block)-memory__size_of_header)) + +#define memory__on_heap_p(block) \ + (memory__get_header(block)->check_field==CHECK_FIELD_VALUE) + +#else + +#define memory__size_of_header 0 + +#define memory__on_heap_p(block) 1 + +#endif + +/* + * int string_Length(string s): + * Effects: Returns the number of non-null characters in s. + */ + +#ifndef memory__PROVIDER +#ifdef DEBUG_MEMORY + +extern char *current_module; +extern void set_module(); + +#define malloc(size) (set_module(__FILE__,__LINE__),\ + memory__malloc(size)) +#define realloc(ptr, size) (set_module(__FILE__,__LINE__),\ + memory__realloc((char *) ptr, size)) +#define calloc(nelem, elsize) (set_module(__FILE__,__LINE__),\ + memory__calloc(nelem, elsize)) +#define free(ptr) (set_module(__FILE__,__LINE__),\ + memory__free((char *) ptr)) +#else + +#define malloc(size) memory__malloc(size) +#define realloc(ptr, size) memory__realloc((char *) ptr, size) +#define calloc(nelem, elsize) memory__calloc(nelem, elsize) +#define free(ptr) memory__free((char *) ptr) + +#endif /* DEBUG_MEMORY */ + +#endif /* memory__PROVIDER */ + +#endif /* memory_MODULE */ + +#endif /* SABER */ diff --git a/zwgc/String/Imakefile b/zwgc/String/Imakefile new file mode 100644 index 0000000..cbc2f47 --- /dev/null +++ b/zwgc/String/Imakefile @@ -0,0 +1,35 @@ +/**/# Copyright 1988 Massachusetts Institute of Technology. +/**/# +/**/# For copying and distribution information, see the file +/**/# "mit-copyright.h". +/**/# +/**/# $Source$ +/**/# $Author$ +/**/# $Header$ +/**/# + +LIBS= $(ZEPHYR_LIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) +LINTLIBS=$(ZEPHYR_LINTLIB) $(COMERR_LIB) $(KRB_LIB) $(DES_LIB) + +OBJS = new_string.o +SRCS = new_string.c +HDRS = new_string.h + +SRCDIR= ${SRCTOP}/zwgc/zwgc.dev/String +CODE= ${SRCS} ${HDRS} Imakefile + +LINCLUDES= -I../Memory + +normal_obj_rule() + +all:: string_spec $(OBJS) + +# +# /* Automatically generate a spec file from the template include file: */ +# +string_spec: new_string.h + cat new_string.h | grep "^.\*" | grep -v "^ \*/" | sed 's/.\*//' > string_spec + +clean:: + $(RM) $(OBJS) memory_spec + diff --git a/zwgc/String/new_string.c b/zwgc/String/new_string.c new file mode 100644 index 0000000..ea0f894 --- /dev/null +++ b/zwgc/String/new_string.c @@ -0,0 +1,197 @@ +/* This file is part of the Project Athena Zephyr Notification System. + * It is one of the source files comprising zwgc, the Zephyr WindowGram + * client. + * + * Created by: Marc Horowitz <marc@athena.mit.edu> + * + * $Source$ + * $Author$ + * + * Copyright (c) 1989 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file + * "mit-copyright.h". + */ + +#if (!defined(lint) && !defined(SABER)) +static char rcsid_new_string_c[] = "$Id$"; +#endif + +/* + * string - a module providing operations on C strings. (i.e., char *'s) + * + * Overview: + * + * A string is a standard C string. I.e., a char pointer to a + * null-terminated sequence of characters. 0 is NOT considered a valid + * string! Various operations are available. See the string_spec file + * for details. + * + * Note: This module assumes that malloc NEVER returns 0 for reasonable + * requests. It is the users responsibility to either ensure that + * this happens or supply a version of malloc with error + * handling. + * + * Some strings are mutable. + */ + +#ifdef DEBUG +#define assert(x) if (!(x)) abort() +#else +#define assert(x) +#endif + +#include <ctype.h> +#include "new_memory.h" + +#include <strings.h> + +#define string_Length(s) strlen(s) +typedef char *string; + +/* + * string string_CreateFromData(char *data, int length): + * Requires: data[0], data[1], ..., data[length-1] != 0 + * Effects: Takes the first length characters at data and + * creates a string containing them. The returned string + * is on the heap & must be freed eventually. + * I.e., if passed "foobar" and 3, it would return + * string_Copy("foo"). + */ + +string string__CreateFromData(data, length) + char *data; + int length; +{ + string result; + + assert(length>=0); + + result = (string)malloc(length+1); + assert(result); + + bcopy(data, result, length); + result[length] = 0; + + return(result); +} + +/* + * string string_Copy(string s): + * Effects: Returns a copy of s on the heap. The copy must be + * freed eventually. + */ + +string string__Copy(s) + string s; +{ + int length; + string result; + + assert(s); + + length = string_Length(s)+1; + result = (string)malloc(length); + assert(result); + + bcopy(s, result, length); + return(result); +} + +/* + * string string_Concat(string a, b): + * Effects: Returns a string equal to a concatenated to b. + * The returned string is on the heap and must be + * freed eventually. I.e., given "abc" and "def", + * returns string_Copy("abcdef"). + */ + +string string__Concat(a, b) + string a, b; +{ + string result; + int a_length, b_size, result_size; + + a_length = string_Length(a); + b_size = string_Length(b)+1; + result_size = a_length+b_size; + result = (string)malloc(result_size); + assert(result); + + bcopy(a, result, a_length); + bcopy(b, result+a_length, b_size); + + return(result); +} + +/* + * string string_Concat2(string a, b): + * Modifies: a + * Requires: a is on the heap, b does not point into a. + * Effects: Equivalent to: + * string temp; + * temp = string_Concat(a,b); + * free(a); + * return(temp); + * only faster. I.e., uses realloc instead of malloc+bcopy. + */ + +string string__Concat2(a, b) + string a, b; +{ + int a_length = string_Length(a); + int b_size = string_Length(b)+1; + +#ifdef DEBUG_MEMORY + assert(memory__on_heap_p(a)); +#endif + + a = (string)realloc(a, a_length+b_size); + assert(a); + bcopy(b, a+a_length, b_size); + + return(a); +} + +/* + * string string_Downcase(string s): + * Modifies: s + * Effects: Modifies s by changing every uppercase character in s + * to the corresponding lowercase character. Nothing else + * is changed. I.e., "FoObAr19." is changed to "foobar19.". + * S is returned as a convenience. + */ + +string string_Downcase(s) + string s; +{ + char *ptr; + + for (ptr=s; *ptr; ptr++) { + if (isupper(*ptr)) + *ptr = tolower(*ptr); + } + + return(s); +} + +/* + * string string_Upcase(string s): + * Modifies: s + * Effects: Modifies s by changing every lowercase character in s + * to the corresponding uppercase character. Nothing else + * is changed. I.e., "FoObAr19." is changed to "FOOBAR19.". + * S is returned as a convenience. + */ + +string string_Upcase(s) + string s; +{ + char *ptr; + + for (ptr=s; *ptr; ptr++) { + if (islower(*ptr)) + *ptr = toupper(*ptr); + } + + return(s); +} diff --git a/zwgc/String/new_string.h b/zwgc/String/new_string.h new file mode 100644 index 0000000..92b3f2e --- /dev/null +++ b/zwgc/String/new_string.h @@ -0,0 +1,136 @@ +/* This file is part of the Project Athena Zephyr Notification System. + * It is one of the source files comprising zwgc, the Zephyr WindowGram + * client. + * + * Created by: Marc Horowitz <marc@athena.mit.edu> + * + * $Source$ + * $Author$ + * $Id$ + * + * Copyright (c) 1989 by the Massachusetts Institute of Technology. + * For copying and distribution information, see the file + * "mit-copyright.h". + */ + +#ifndef string_TYPE +#define string_TYPE + +#include <strings.h> +#include "new_memory.h" + +typedef char *string; + +/* + * int string_Length(string s): + * Effects: Returns the number of non-null characters in s. + */ + +#define string_Length(s) strlen(s) + +/* + * int string_Eq(string a, b): + * Effects: Returns true iff strings a & b are equal. I.e., have the + * same character contents. + */ + +#define string_Eq(a,b) (!strcmp(a,b)) + +/* + * int string_Neq(string a, b): + * Effects: Returns true iff strings a & b are not equal. + */ + +#define string_Neq(a,b) (strcmp(a,b)) + +/* + * string string_CreateFromData(char *data, int length): + * Requires: data[0], data[1], ..., data[length-1] != 0 + * Effects: Takes the first length characters at data and + * creates a string containing them. The returned string + * is on the heap & must be freed eventually. + * I.e., if passed "foobar" and 3, it would return + * string_Copy("foo"). + */ + +extern string string__CreateFromData(); +#ifdef DEBUG_MEMORY +#define string_CreateFromData(data,length) (set_module(__FILE__,__LINE__),\ + string__CreateFromData(data,length)) +#else +#define string_CreateFromData(data,length) string__CreateFromData(data,length) +#endif + +/* + * string string_Copy(string s): + * Effects: Returns a copy of s on the heap. The copy must be + * freed eventually. + */ + +extern string string__Copy(/* string s */); +#ifdef DEBUG_MEMORY +#define string_Copy(data) (set_module(__FILE__,__LINE__),\ + string__Copy(data)) +#else +#define string_Copy(data) string__Copy(data) +#endif + +/* + * string string_Concat(string a, b): + * Effects: Returns a string equal to a concatenated to b. + * The returned string is on the heap and must be + * freed eventually. I.e., given "abc" and "def", + * returns string_Copy("abcdef"). + */ + +extern string string__Concat(/* string a, b */); +#ifdef DEBUG_MEMORY +#define string_Concat(a,b) (set_module(__FILE__,__LINE__),\ + string__Concat(a,b)) +#else +#define string_Concat(a,b) string__Concat(a,b) +#endif + +/* + * string string_Concat2(string a, b): + * Modifies: a + * Requires: a is on the heap, b does not point into a. + * Effects: Equivalent to: + * string temp; + * temp = string_Concat(a,b); + * free(a); + * return(temp); + * only faster. I.e., uses realloc instead of malloc+bcopy. + */ + +extern string string__Concat2(/* string a, b */); +#ifdef DEBUG_MEMORY +#define string_Concat2(a,b) (set_module(__FILE__,__LINE__),\ + string__Concat2(a,b)) +#else +#define string_Concat2(a,b) string__Concat2(a,b) +#endif + +/* + * string string_Downcase(string s): + * Modifies: s + * Effects: Modifies s by changing every uppercase character in s + * to the corresponding lowercase character. Nothing else + * is changed. I.e., "FoObAr19." is changed to "foobar19.". + * S is returned as a convenience. + */ + +extern string string_Downcase(); + +/* + * string string_Upcase(string s): + * Modifies: s + * Effects: Modifies s by changing every lowercase character in s + * to the corresponding uppercase character. Nothing else + * is changed. I.e., "FoObAr19." is changed to "FOOBAR19.". + * S is returned as a convenience. + */ + +extern string string_Upcase(); + +#endif |