Fixes #1742.
add_library(rbtree STATIC
# Header files
- misc.h
red_black_tree.h
stack.h
AM_CPPFLAGS = -I$(top_srcdir)
-noinst_HEADERS = misc.h red_black_tree.h stack.h
+noinst_HEADERS = red_black_tree.h stack.h
noinst_LTLIBRARIES = librbtree_C.la
librbtree_C_la_SOURCES = misc.c red_black_tree.c stack.c
* See the LICENSE file for copyright information. *
**********************************************************/
-#include "config.h"
+#include "red_black_tree.h"
-#include "misc.h"
-#include <stdio.h>
-
-jmp_buf rb_jbuf;
-
-/***********************************************************************/
-/* FUNCTION: SafeMalloc */
-/**/
-/* INPUTS: size is the size to malloc */
-/**/
-/* OUTPUT: returns pointer to allocated memory if successful */
-/**/
-/* EFFECT: mallocs new memory. If malloc fails, prints error message */
-/* and terminates program. */
-/**/
-/* Modifies Input: none */
-/**/
-/***********************************************************************/
-
-void * SafeMalloc(size_t size) {
- void * result;
-
- if ( (result = malloc(size)) ) { /* assignment intentional */
- return(result);
- } else {
- fprintf(stderr, "memory overflow: malloc failed in SafeMalloc.");
- /* printf(" Exiting Program.\n"); */
- longjmp(rb_jbuf, 2);
- return(0);
- }
-}
/* NullFunction does nothing it is included so that it can be passed */
/* as a function to RBTreeCreate when no other suitable function has */
/* been defined */
+++ /dev/null
-/* $Id$Revision: */
-/* vim:set shiftwidth=4 ts=8: */
-
-/**********************************************************
-* See the LICENSE file for copyright information. *
-**********************************************************/
-
-#ifndef INC_E_MISC_
-#define INC_E_MISC_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdlib.h>
-#include <setjmp.h>
-
-extern jmp_buf rb_jbuf;
-
-/* CONVENTIONS: All data structures for red-black trees have the prefix */
-/* "rb_" to prevent name conflicts. */
-/* */
-/* Function names: Each word in a function name begins with */
-/* a capital letter. An example funcntion name is */
-/* CreateRedTree(a,b,c). Furthermore, each function name */
-/* should begin with a capital letter to easily distinguish */
-/* them from variables. */
-/* */
-/* Variable names: Each word in a variable name begins with */
-/* a capital letter EXCEPT the first letter of the variable */
-/* name. For example, int newLongInt. Global variables have */
-/* names beginning with "g". An example of a global */
-/* variable name is gNewtonsConstant. */
-
-void * SafeMalloc(size_t size);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
-
<Lib />
</ItemDefinitionGroup>
<ItemGroup>
- <ClInclude Include="misc.h" />
<ClInclude Include="red_black_tree.h" />
<ClInclude Include="stack.h" />
</ItemGroup>
</Filter>
</ItemGroup>
<ItemGroup>
- <ClInclude Include="misc.h">
- <Filter>Header Files</Filter>
- </ClInclude>
<ClInclude Include="red_black_tree.h">
<Filter>Header Files</Filter>
</ClInclude>
#include <assert.h>
#include "red_black_tree.h"
#include "stdio.h"
+#include <stdlib.h>
/***********************************************************************/
/* FUNCTION: RBTreeCreate */
rb_red_blk_tree* newTree = NULL;
rb_red_blk_node* temp;
- if (setjmp(rb_jbuf)) {
- if (newTree) {
- if (newTree->nil) free (newTree->nil);
- free (newTree);
- }
+ newTree= malloc(sizeof(rb_red_blk_tree));
+ if (newTree == NULL) {
return NULL;
}
- newTree=(rb_red_blk_tree*) SafeMalloc(sizeof(rb_red_blk_tree));
newTree->nil = newTree->root = NULL;
newTree->Compare= CompFunc;
newTree->DestroyKey= DestFunc;
/* see the comment in the rb_red_blk_tree structure in red_black_tree.h */
/* for information on nil and root */
- temp=newTree->nil= (rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node));
+ temp=newTree->nil= malloc(sizeof(rb_red_blk_node));
+ if (temp == NULL) {
+ free(newTree);
+ return NULL;
+ }
temp->parent=temp->left=temp->right=temp;
temp->red=0;
temp->key=0;
- temp=newTree->root= (rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node));
+ temp=newTree->root= malloc(sizeof(rb_red_blk_node));
+ if (temp == NULL) {
+ free(newTree->nil);
+ free(newTree);
+ return NULL;
+ }
temp->parent=temp->left=temp->right=newTree->nil;
temp->key=0;
temp->red=0;
rb_red_blk_node * x;
rb_red_blk_node * newNode;
- if (setjmp(rb_jbuf))
+ x= malloc(sizeof(rb_red_blk_node));
+ if (x == NULL) {
return NULL;
- x=(rb_red_blk_node*) SafeMalloc(sizeof(rb_red_blk_node));
+ }
x->key=key;
x->info=info;
rb_red_blk_node* x=tree->root->left;
rb_red_blk_node* lastBest=nil;
- if (setjmp(rb_jbuf)) {
+ enumResultStack=StackCreate();
+ if (enumResultStack == NULL) {
return NULL;
}
- enumResultStack=StackCreate();
while(nil != x) {
if ( 1 == (tree->Compare(x->key,high)) ) { /* x->key > high */
x=x->left;
}
}
while ( (lastBest != nil) && (1 != tree->Compare(low,lastBest->key))) {
- StackPush(enumResultStack,lastBest);
+ if (StackPush(enumResultStack,lastBest) != 0) {
+ StackDestroy(enumResultStack, NullFunction);
+ return NULL;
+ }
lastBest=TreePredecessor(tree,lastBest);
}
return(enumResultStack);
extern "C" {
#endif
-#include "misc.h"
#include "stack.h"
/* CONVENTIONS: All data structures for red-black trees have the prefix */
#include "config.h"
#include "stack.h"
+#include <stdlib.h>
intptr_t StackNotEmpty(stk_stack * theStack) {
return( theStack ? (intptr_t) theStack->top : 0);
stk_stack * StackCreate() {
stk_stack * newStack;
- newStack=(stk_stack *) SafeMalloc(sizeof(stk_stack));
+ newStack= malloc(sizeof(stk_stack));
+ if (newStack == NULL) {
+ return NULL;
+ }
newStack->top=newStack->tail=NULL;
return(newStack);
}
-void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) {
+int StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer) {
stk_stack_node * newNode;
if(!theStack->top) {
- newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node));
+ newNode= malloc(sizeof(stk_stack_node));
+ if (newNode == NULL) {
+ return -1;
+ }
newNode->info=newInfoPointer;
newNode->next=theStack->top;
theStack->top=newNode;
theStack->tail=newNode;
} else {
- newNode=(stk_stack_node *) SafeMalloc(sizeof(stk_stack_node));
+ newNode= malloc(sizeof(stk_stack_node));
+ if (newNode == NULL) {
+ return -1;
+ }
newNode->info=newInfoPointer;
newNode->next=theStack->top;
theStack->top=newNode;
}
-
+
+ return 0;
}
DATA_TYPE StackPop(stk_stack * theStack) {
#include "config.h"
#include <stdint.h>
-#include "misc.h"
-
/* CONVENTIONS: All data structures for stacks have the prefix */
/* "stk_" to prevent name conflicts. */
/* */
/* I didn't think additional comments would be useful */
stk_stack * StackJoin(stk_stack * stack1, stk_stack * stack2);
stk_stack * StackCreate(void);
-void StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer);
+
+/** push an item onto the stack
+ *
+ * @param theStack Stack to operate on
+ * @param newInfoPointer Item to push onto the top
+ * @return 0 on success
+ */
+int StackPush(stk_stack * theStack, DATA_TYPE newInfoPointer);
+
void * StackPop(stk_stack * theStack);
intptr_t StackNotEmpty(stk_stack *);