You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/02/24 00:56:07 UTC

[lucy-commits] svn commit: r1073991 - in /incubator/lucy/trunk/clownfish/src: CFCBase.c CFCCBlock.c CFCClass.c CFCDocuComment.c CFCFile.c CFCMethod.c CFCParamList.c CFCParcel.c CFCSymbol.c CFCType.c CFCUtil.c CFCUtil.h CFCVariable.c

Author: marvin
Date: Wed Feb 23 23:56:07 2011
New Revision: 1073991

URL: http://svn.apache.org/viewvc?rev=1073991&view=rev
Log:
Wrap all memory allocation operations within the Clownfish compiler, so that
it is no longer necessary to check return values after each call.  The
wrapping code in CFCUtil is adapted from code taken from
trunk/core/Lucy/Util/Memory.*.

Modified:
    incubator/lucy/trunk/clownfish/src/CFCBase.c
    incubator/lucy/trunk/clownfish/src/CFCCBlock.c
    incubator/lucy/trunk/clownfish/src/CFCClass.c
    incubator/lucy/trunk/clownfish/src/CFCDocuComment.c
    incubator/lucy/trunk/clownfish/src/CFCFile.c
    incubator/lucy/trunk/clownfish/src/CFCMethod.c
    incubator/lucy/trunk/clownfish/src/CFCParamList.c
    incubator/lucy/trunk/clownfish/src/CFCParcel.c
    incubator/lucy/trunk/clownfish/src/CFCSymbol.c
    incubator/lucy/trunk/clownfish/src/CFCType.c
    incubator/lucy/trunk/clownfish/src/CFCUtil.c
    incubator/lucy/trunk/clownfish/src/CFCUtil.h
    incubator/lucy/trunk/clownfish/src/CFCVariable.c

Modified: incubator/lucy/trunk/clownfish/src/CFCBase.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBase.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBase.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCBase.c Wed Feb 23 23:56:07 2011
@@ -26,10 +26,7 @@
 CFCBase*
 CFCBase_allocate(size_t size, const char *klass)
 {
-    CFCBase *self = (CFCBase*)calloc(size, 1);
-    if (!self) {
-        croak("Failed to calloc %" UVuf " bytes", (UV)size);
-    }
+    CFCBase *self = (CFCBase*)CALLOCATE(size, 1);
     self->perl_obj = CFCUtil_make_perl_obj(self, klass);
     return self;
 }
@@ -37,7 +34,7 @@ CFCBase_allocate(size_t size, const char
 void
 CFCBase_destroy(CFCBase *self)
 {
-    free(self);
+    FREEMEM(self);
 }
 
 CFCBase*

Modified: incubator/lucy/trunk/clownfish/src/CFCCBlock.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCCBlock.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCCBlock.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCCBlock.c Wed Feb 23 23:56:07 2011
@@ -48,7 +48,7 @@ CFCCBlock_init(CFCCBlock *self, const ch
 void
 CFCCBlock_destroy(CFCCBlock *self)
 {
-    free(self->contents);
+    FREEMEM(self->contents);
     CFCBase_destroy((CFCBase*)self);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCClass.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCClass.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCClass.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCClass.c Wed Feb 23 23:56:07 2011
@@ -60,7 +60,6 @@ CFCClass_new(struct CFCParcel *parcel, c
 {
     CFCClass *self = (CFCClass*)CFCBase_allocate(sizeof(CFCClass),
         "Clownfish::Class");
-    if (!self) { croak("malloc failed"); }
     return CFCClass_init(self, parcel, exposure, class_name, class_cnick,
         micro_sym, docucomment, source_class, parent_class_name, is_final, 
         is_inert);
@@ -77,7 +76,7 @@ CFCClass_init(CFCClass *self, struct CFC
         class_cnick, micro_sym);
     self->parent     = NULL;
     self->tree_grown = false;
-    self->autocode   = (char*)calloc(1, sizeof(char));
+    self->autocode   = (char*)CALLOCATE(1, sizeof(char));
     self->parent_class_name = CFCUtil_strdup(parent_class_name);
     self->docucomment 
         = (CFCDocuComment*)CFCBase_incref((CFCBase*)docucomment);
@@ -95,15 +94,10 @@ CFCClass_init(CFCClass *self, struct CFC
     const char *prefix = CFCSymbol_get_prefix((CFCSymbol*)self);
     size_t prefix_len = strlen(prefix);
     size_t struct_sym_len = strlen(self->struct_sym);
-    self->short_vtable_var = (char*)malloc(struct_sym_len + 1);
-    self->full_struct_sym  = (char*)malloc(prefix_len + struct_sym_len + 1);
-    self->full_vtable_var  = (char*)malloc(prefix_len + struct_sym_len + 1);
-    self->full_vtable_type = (char*)malloc(prefix_len + struct_sym_len + 3 + 1);
-    if (   !self->full_struct_sym || !self->short_vtable_var 
-        || !self->full_vtable_var || !self->full_vtable_type
-    ) {
-        croak("malloc failed");
-    }
+    self->short_vtable_var = (char*)MALLOCATE(struct_sym_len + 1);
+    self->full_struct_sym  = (char*)MALLOCATE(prefix_len + struct_sym_len + 1);
+    self->full_vtable_var  = (char*)MALLOCATE(prefix_len + struct_sym_len + 1);
+    self->full_vtable_type = (char*)MALLOCATE(prefix_len + struct_sym_len + 3 + 1);
     size_t i;
     for (i = 0; i < struct_sym_len; i++) {
         self->short_vtable_var[i] = toupper(self->struct_sym[i]);
@@ -121,8 +115,7 @@ CFCClass_init(CFCClass *self, struct CFC
 
     // Cache the relative path to the autogenerated C header file.
     size_t source_class_len = strlen(self->source_class);
-    self->include_h = (char*)malloc(source_class_len + 3);
-    if (!self->include_h) { croak("malloc failed"); }
+    self->include_h = (char*)MALLOCATE(source_class_len + 3);
     int j;
     for (i = 0, j = 0; i < source_class_len; i++) {
         if (self->source_class[i] == ':') {
@@ -147,14 +140,14 @@ CFCClass_destroy(CFCClass *self)
 {
     CFCBase_decref((CFCBase*)self->docucomment);
     CFCBase_decref((CFCBase*)self->parent);
-    free(self->autocode);
-    free(self->source_class);
-    free(self->parent_class_name);
-    free(self->struct_sym);
-    free(self->short_vtable_var);
-    free(self->full_struct_sym);
-    free(self->full_vtable_var);
-    free(self->full_vtable_type);
+    FREEMEM(self->autocode);
+    FREEMEM(self->source_class);
+    FREEMEM(self->parent_class_name);
+    FREEMEM(self->struct_sym);
+    FREEMEM(self->short_vtable_var);
+    FREEMEM(self->full_struct_sym);
+    FREEMEM(self->full_vtable_var);
+    FREEMEM(self->full_vtable_type);
     CFCSymbol_destroy((CFCSymbol*)self);
 }
 
@@ -193,8 +186,7 @@ void
 CFCClass_append_autocode(CFCClass *self, const char *autocode)
 {
     size_t size = strlen(self->autocode) + strlen(autocode) + 1;
-    self->autocode = (char*)realloc(self->autocode, size);
-    if (!self->autocode) { croak("realloc failed"); }
+    self->autocode = (char*)REALLOCATE(self->autocode, size);
     strcat(self->autocode, autocode);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCDocuComment.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCDocuComment.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCDocuComment.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCDocuComment.c Wed Feb 23 23:56:07 2011
@@ -41,10 +41,7 @@ static void
 S_strip(char *comment)
 {
     size_t len = strlen(comment);
-    char *scratch = (char*)malloc(len + 1);
-    if (!scratch) {
-        croak("malloc failed");
-    }
+    char *scratch = (char*)MALLOCATE(len + 1);
 
     // Establish that comment text begins with "/**" and ends with "*/".
     if (   strstr(comment, "/**") != comment
@@ -82,7 +79,7 @@ S_strip(char *comment)
     comment[j] = '\0';
 
     // Clean up.
-    free(scratch);
+    FREEMEM(scratch);
 }
 
 CFCDocuComment*
@@ -123,11 +120,8 @@ CFCDocuComment_parse(const char *raw_tex
 
     // Extract @param directives.
     size_t num_params = 0;
-    self->param_names = (char**)calloc(num_params + 1, sizeof(char*));
-    self->param_docs  = (char**)calloc(num_params + 1, sizeof(char*));
-    if (!self->param_names || !self->param_docs) { 
-        croak("calloc failed"); 
-    }
+    self->param_names = (char**)CALLOCATE(num_params + 1, sizeof(char*));
+    self->param_docs  = (char**)CALLOCATE(num_params + 1, sizeof(char*));
     {
         char *candidate = strstr(text, "@param");
         size_t len = strlen(text);
@@ -166,11 +160,8 @@ CFCDocuComment_parse(const char *raw_tex
 
             num_params++;
             size_t size = (num_params + 1) * sizeof(char*);
-            self->param_names = realloc(self->param_names, size);
-            self->param_docs  = realloc(self->param_docs, size);
-            if (!self->param_names || !self->param_docs) { 
-                croak("realloc failed"); 
-            }
+            self->param_names = REALLOCATE(self->param_names, size);
+            self->param_docs  = REALLOCATE(self->param_docs, size);
             self->param_names[num_params - 1] 
                 = CFCUtil_strndup(param_name, param_name_len);
             self->param_docs[num_params - 1] 
@@ -218,7 +209,7 @@ CFCDocuComment_parse(const char *raw_tex
         CFCUtil_trim_whitespace(self->retval);
     }
 
-    free(text);
+    FREEMEM(text);
     return self;
 }
 
@@ -228,20 +219,20 @@ CFCDocuComment_destroy(CFCDocuComment *s
     size_t i;
     if (self->param_names) {
         for (i = 0; self->param_names[i] != NULL; i++) {
-            free(self->param_names[i]);
+            FREEMEM(self->param_names[i]);
         }
-        free(self->param_names);
+        FREEMEM(self->param_names);
     }
     if (self->param_docs) {
         for (i = 0; self->param_docs[i] != NULL; i++) {
-            free(self->param_docs[i]);
+            FREEMEM(self->param_docs[i]);
         }
-        free(self->param_docs);
+        FREEMEM(self->param_docs);
     }
-    free(self->description);
-    free(self->brief);
-    free(self->long_des);
-    free(self->retval);
+    FREEMEM(self->description);
+    FREEMEM(self->brief);
+    FREEMEM(self->long_des);
+    FREEMEM(self->retval);
     CFCBase_destroy((CFCBase*)self);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCFile.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCFile.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCFile.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCFile.c Wed Feb 23 23:56:07 2011
@@ -65,19 +65,15 @@ CFCFile_init(CFCFile *self, const char *
     CFCUTIL_NULL_CHECK(source_class);
     self->modified = false;
     self->source_class = CFCUtil_strdup(source_class);
-    self->blocks = (CFCBase**)calloc(1, sizeof(CFCBase*));
-    self->classes = (CFCClass**)calloc(1, sizeof(CFCBase*));
-    if (!self->blocks || !self->classes) { croak("malloc failed"); }
+    self->blocks = (CFCBase**)CALLOCATE(1, sizeof(CFCBase*));
+    self->classes = (CFCClass**)CALLOCATE(1, sizeof(CFCBase*));
 
     // Derive include guard name, plus C code for opening and closing the
     // guard.
     size_t len = strlen(source_class);
-    self->guard_name = (char*)malloc(len + sizeof("H_") + 1);
-    self->guard_start = (char*)malloc(len * 2 + 40);
-    self->guard_close = (char*)malloc(len + 20);
-    if (!self->guard_name || !self->guard_start || !self->guard_close) {
-        croak("malloc failed");
-    }
+    self->guard_name = (char*)MALLOCATE(len + sizeof("H_") + 1);
+    self->guard_start = (char*)MALLOCATE(len * 2 + 40);
+    self->guard_close = (char*)MALLOCATE(len + 20);
     memcpy(self->guard_name, "H_", 2);
     size_t i, j;
     for (i = 0, j = 2; i < len; i++, j++) {
@@ -99,8 +95,7 @@ CFCFile_init(CFCFile *self, const char *
     if (check < 0) { croak("sprintf failed"); }
 
     // Cache partial path derived from source_class.
-    self->path_part = malloc(len + 1);
-    if (!self->path_part) { croak("malloc failed"); }
+    self->path_part = MALLOCATE(len + 1);
     for (i = 0, j = 0; i < len; i++, j++) {
         char c = source_class[i];
         if (c == ':') {
@@ -124,17 +119,17 @@ CFCFile_destroy(CFCFile *self)
     for (i = 0; self->blocks[i] != NULL; i++) {
         CFCBase_decref(self->blocks[i]);
     }
-    free(self->blocks);
+    FREEMEM(self->blocks);
     for (i = 0; self->classes[i] != NULL; i++) {
         CFCBase_decref((CFCBase*)self->classes[i]);
     }
     */
-    free(self->classes);
-    free(self->guard_name);
-    free(self->guard_start);
-    free(self->guard_close);
-    free(self->source_class);
-    free(self->path_part);
+    FREEMEM(self->classes);
+    FREEMEM(self->guard_name);
+    FREEMEM(self->guard_start);
+    FREEMEM(self->guard_close);
+    FREEMEM(self->source_class);
+    FREEMEM(self->path_part);
     CFCBase_destroy((CFCBase*)self);
 }
 
@@ -152,8 +147,7 @@ CFCFile_add_block(CFCFile *self, CFCBase
         }
         num_class_blocks++;
         size_t size = (num_class_blocks + 1) * sizeof(CFCClass*);
-        self->classes = (CFCClass**)realloc(self->classes, size);
-        if (!self->classes) { croak("realloc failed"); }
+        self->classes = (CFCClass**)REALLOCATE(self->classes, size);
         self->classes[num_class_blocks - 1] 
             = (CFCClass*)CFCBase_incref(block);
         self->classes[num_class_blocks] = NULL;
@@ -170,8 +164,7 @@ CFCFile_add_block(CFCFile *self, CFCBase
         }
         num_blocks++;
         size_t size = (num_blocks + 1) * sizeof(CFCBase*);
-        self->blocks = (CFCBase**)realloc(self->blocks, size);
-        if (!self->blocks) { croak("realloc failed"); }
+        self->blocks = (CFCBase**)REALLOCATE(self->blocks, size);
         self->blocks[num_blocks - 1] = CFCBase_incref(block);
         self->blocks[num_blocks] = NULL;
     }

Modified: incubator/lucy/trunk/clownfish/src/CFCMethod.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCMethod.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCMethod.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCMethod.c Wed Feb 23 23:56:07 2011
@@ -103,7 +103,7 @@ CFCMethod_init(CFCMethod *self, CFCParce
     CFCFunction_init((CFCFunction*)self, parcel, exposure, class_name,
         class_cnick, micro_sym, return_type, param_list, docucomment,
         false);
-    free(micro_sym);
+    FREEMEM(micro_sym);
 
     // Verify that the first element in the arg list is a self.
     CFCVariable **args = CFCParamList_get_variables(param_list);
@@ -113,11 +113,10 @@ CFCMethod_init(CFCMethod *self, CFCParce
     const char *prefix    = CFCSymbol_get_prefix((CFCSymbol*)self);
     const char *last_colon = strrchr(class_name, ':');
     const char *struct_sym = last_colon ? last_colon + 1 : class_name;
-    char *wanted = (char*)malloc(strlen(prefix) + strlen(struct_sym) + 1);
-    if (!wanted) { croak("malloc failed"); }
+    char *wanted = (char*)MALLOCATE(strlen(prefix) + strlen(struct_sym) + 1);
     sprintf(wanted, "%s%s", prefix, struct_sym);
     int mismatch = strcmp(wanted, specifier);
-    free(wanted);
+    FREEMEM(wanted);
     if (mismatch) { 
         croak("First arg type doesn't match class: '%s' '%s", class_name,
             specifier);
@@ -132,11 +131,8 @@ CFCMethod_init(CFCMethod *self, CFCParce
     // Derive more symbols.
     const char *full_func_sym = CFCFunction_full_func_sym((CFCFunction*)self);
     size_t amount = strlen(full_func_sym) + sizeof("_OVERRIDE") + 1;
-    self->full_callback_sym = (char*)malloc(amount);
-    self->full_override_sym = (char*)malloc(amount);
-    if (!self->full_callback_sym || !self->full_override_sym) {
-        croak("malloc failed");
-    }
+    self->full_callback_sym = (char*)MALLOCATE(amount);
+    self->full_override_sym = (char*)MALLOCATE(amount);
     int check = sprintf(self->full_callback_sym, "%s_CALLBACK", 
         full_func_sym);
     if (check < 0) { croak("sprintf failed"); }
@@ -149,10 +145,10 @@ CFCMethod_init(CFCMethod *self, CFCParce
 
     // Cache typedef.
     const char *short_sym = CFCSymbol_short_sym((CFCSymbol*)self);
-    char *short_typedef = (char*)malloc(strlen(short_sym) + 3);
+    char *short_typedef = (char*)MALLOCATE(strlen(short_sym) + 3);
     sprintf(short_typedef, "%s_t", short_sym);
     CFCMethod_set_short_typedef(self, short_typedef);
-    free(short_typedef);
+    FREEMEM(short_typedef);
 
     return self;
 }
@@ -160,11 +156,11 @@ CFCMethod_init(CFCMethod *self, CFCParce
 void
 CFCMethod_destroy(CFCMethod *self)
 {
-    free(self->macro_sym);
-    free(self->short_typedef);
-    free(self->full_typedef);
-    free(self->full_callback_sym);
-    free(self->full_override_sym);
+    FREEMEM(self->macro_sym);
+    FREEMEM(self->short_typedef);
+    FREEMEM(self->full_typedef);
+    FREEMEM(self->full_callback_sym);
+    FREEMEM(self->full_override_sym);
     CFCFunction_destroy((CFCFunction*)self);
 }
 
@@ -219,14 +215,13 @@ CFCMethod_get_macro_sym(CFCMethod *self)
 void
 CFCMethod_set_short_typedef(CFCMethod *self, const char *short_typedef)
 {
-    free(self->short_typedef);
-    free(self->full_typedef);
+    FREEMEM(self->short_typedef);
+    FREEMEM(self->full_typedef);
     if (short_typedef) {
         self->short_typedef = CFCUtil_strdup(short_typedef);
         const char *prefix = CFCSymbol_get_prefix((CFCSymbol*)self);
         size_t amount = strlen(prefix) + strlen(short_typedef) + 1;
-        self->full_typedef = (char*)malloc(amount);
-        if (!self->full_typedef) { croak("malloc failed"); }
+        self->full_typedef = (char*)MALLOCATE(amount);
         int check = sprintf(self->full_typedef, "%s%s", prefix,
             short_typedef);
         if (check < 0) { croak("sprintf failed"); }

Modified: incubator/lucy/trunk/clownfish/src/CFCParamList.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParamList.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParamList.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParamList.c Wed Feb 23 23:56:07 2011
@@ -53,8 +53,8 @@ CFCParamList_init(CFCParamList *self, in
 {
     self->variadic  = variadic;
     self->num_vars  = 0;
-    self->variables = (CFCVariable**)calloc(1, sizeof(void*));
-    self->values    = (char**)calloc(1, sizeof(char*));
+    self->variables = (CFCVariable**)CALLOCATE(1, sizeof(void*));
+    self->values    = (char**)CALLOCATE(1, sizeof(char*));
     S_generate_c_strings(self);
     return self;
 }
@@ -66,11 +66,8 @@ CFCParamList_add_param(CFCParamList *sel
     CFCUTIL_NULL_CHECK(variable);
     self->num_vars++;
     size_t amount = (self->num_vars + 1) * sizeof(void*);
-    self->variables = (CFCVariable**)realloc(self->variables, amount);
-    self->values    = (char**)realloc(self->values, amount);
-    if (!self->variables || !self->values) {
-        croak("realloc failed.");
-    }
+    self->variables = (CFCVariable**)REALLOCATE(self->variables, amount);
+    self->values    = (char**)REALLOCATE(self->values, amount);
     self->variables[self->num_vars - 1] 
         = (CFCVariable*)CFCBase_incref((CFCBase*)variable);
     self->values[self->num_vars - 1] = value ? CFCUtil_strdup(value) : NULL;
@@ -86,12 +83,12 @@ CFCParamList_destroy(CFCParamList *self)
     size_t i;
     for (i = 0; i < self->num_vars; i++) {
         CFCBase_decref((CFCBase*)self->variables[i]);
-        free(self->values[i]);
+        FREEMEM(self->values[i]);
     }
-    free(self->variables);
-    free(self->values);
-    free(self->c_string);
-    free(self->name_list);
+    FREEMEM(self->variables);
+    FREEMEM(self->values);
+    FREEMEM(self->c_string);
+    FREEMEM(self->name_list);
     CFCBase_destroy((CFCBase*)self);
 }
 
@@ -113,11 +110,10 @@ S_generate_c_strings(CFCParamList *self)
     if (self->variadic) {
         c_string_size += sizeof(", ...");
     }
-    free(self->c_string);
-    free(self->name_list);
-    self->c_string  = malloc(c_string_size);
-    self->name_list = malloc(name_list_size);
-    if (!self->c_string || !self->name_list) { croak("malloc failed"); }
+    FREEMEM(self->c_string);
+    FREEMEM(self->name_list);
+    self->c_string  = MALLOCATE(c_string_size);
+    self->name_list = MALLOCATE(name_list_size);
     self->c_string[0] = '\0';
     self->name_list[0] = '\0';
 

Modified: incubator/lucy/trunk/clownfish/src/CFCParcel.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCParcel.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCParcel.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCParcel.c Wed Feb 23 23:56:07 2011
@@ -138,12 +138,9 @@ CFCParcel_init(CFCParcel *self, const ch
     size_t cnick_len  = strlen(self->cnick);
     size_t prefix_len = cnick_len ? cnick_len + 1 : 0;
     size_t amount     = prefix_len + 1;
-    self->prefix = (char*)malloc(amount);
-    self->Prefix = (char*)malloc(amount);
-    self->PREFIX = (char*)malloc(amount);
-    if (!self->prefix || !self->Prefix || !self->PREFIX) {
-        croak("malloc failed");
-    }
+    self->prefix = (char*)MALLOCATE(amount);
+    self->Prefix = (char*)MALLOCATE(amount);
+    self->PREFIX = (char*)MALLOCATE(amount);
     memcpy(self->Prefix, self->cnick, cnick_len);
     if (cnick_len) {
         self->Prefix[cnick_len]  = '_';
@@ -167,11 +164,11 @@ CFCParcel_init(CFCParcel *self, const ch
 void
 CFCParcel_destroy(CFCParcel *self)
 {
-    free(self->name);
-    free(self->cnick);
-    free(self->prefix);
-    free(self->Prefix);
-    free(self->PREFIX);
+    FREEMEM(self->name);
+    FREEMEM(self->cnick);
+    FREEMEM(self->prefix);
+    FREEMEM(self->Prefix);
+    FREEMEM(self->PREFIX);
     CFCBase_destroy((CFCBase*)self);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCSymbol.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCSymbol.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCSymbol.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCSymbol.c Wed Feb 23 23:56:07 2011
@@ -179,7 +179,7 @@ CFCSymbol_init(CFCSymbol *self, struct C
     size_t short_sym_len = class_cnick_len
                          + strlen("_") 
                          + strlen(self->micro_sym);
-    self->short_sym = (char*)malloc(short_sym_len + 1);
+    self->short_sym = (char*)MALLOCATE(short_sym_len + 1);
     if (self->class_cnick) {
         memcpy((void*)self->short_sym, self->class_cnick, class_cnick_len);
     }
@@ -192,7 +192,7 @@ CFCSymbol_init(CFCSymbol *self, struct C
     const char *prefix = CFCParcel_get_prefix(self->parcel);
     size_t prefix_len = strlen(prefix);
     size_t full_sym_len = prefix_len + short_sym_len;
-    self->full_sym = (char*)malloc(full_sym_len + 1);
+    self->full_sym = (char*)MALLOCATE(full_sym_len + 1);
     memcpy(self->full_sym, prefix, prefix_len);
     memcpy(&self->full_sym[prefix_len], self->short_sym, short_sym_len);
     self->full_sym[full_sym_len] = '\0';
@@ -204,12 +204,12 @@ void
 CFCSymbol_destroy(CFCSymbol *self)
 {
     CFCBase_decref((CFCBase*)self->parcel);
-    free(self->exposure);
-    free(self->class_name);
-    free(self->class_cnick);
-    free(self->micro_sym);
-    free(self->short_sym);
-    free(self->full_sym);
+    FREEMEM(self->exposure);
+    FREEMEM(self->class_name);
+    FREEMEM(self->class_cnick);
+    FREEMEM(self->micro_sym);
+    FREEMEM(self->short_sym);
+    FREEMEM(self->full_sym);
     CFCBase_destroy((CFCBase*)self);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCType.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCType.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCType.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCType.c Wed Feb 23 23:56:07 2011
@@ -248,8 +248,7 @@ CFCType_new_composite(int flags, CFCType
     // Record array spec.
     const char *array_spec = array ? array : "";
     size_t array_spec_size = strlen(array_spec) + 1;
-    self->array = (char*)malloc(array_spec_size);
-    if (!self->array) { croak("Malloc failed"); }
+    self->array = (char*)MALLOCATE(array_spec_size);
     strcpy(self->array, array_spec);
 
     return self;
@@ -312,9 +311,9 @@ CFCType_destroy(CFCType *self)
         CFCBase_decref((CFCBase*)self->child);
     }
     CFCBase_decref((CFCBase*)self->parcel);
-    free(self->specifier);
-    free(self->c_string);
-    free(self->array);
+    FREEMEM(self->specifier);
+    FREEMEM(self->c_string);
+    FREEMEM(self->array);
     CFCBase_destroy((CFCBase*)self);
 }
 
@@ -369,7 +368,7 @@ CFCType_similar(CFCType *self, CFCType *
 void
 CFCType_set_specifier(CFCType *self, const char *specifier)
 {
-    free(self->specifier);
+    FREEMEM(self->specifier);
     self->specifier = CFCUtil_strdup(specifier);
 }
 
@@ -394,7 +393,7 @@ CFCType_get_parcel(CFCType *self)
 void
 CFCType_set_c_string(CFCType *self, const char *c_string)
 {
-    free(self->c_string);
+    FREEMEM(self->c_string);
     self->c_string = CFCUtil_strdup(c_string);
 }
 

Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.c Wed Feb 23 23:56:07 2011
@@ -90,3 +90,62 @@ CFCUtil_trim_whitespace(char *text)
     *text = '\0';
 }
 
+void*
+CFCUtil_wrapped_malloc(size_t count, const char *file, int line)
+{
+    void *pointer = malloc(count);
+    if (pointer == NULL && count != 0) {
+        if (sizeof(long) >= sizeof(size_t)) {
+            fprintf(stderr, "Can't malloc %lu bytes at %s line %d\n", 
+                (unsigned long)count, file, line);
+        }
+        else {
+            fprintf(stderr, "malloc failed at %s line %d\n", file, line);
+        }
+        exit(1);
+    }
+    return pointer;
+}
+
+void*
+CFCUtil_wrapped_calloc(size_t count, size_t size, const char *file, int line)
+{
+    void *pointer = calloc(count, size);
+    if (pointer == NULL && count != 0) {
+        if (sizeof(long) >= sizeof(size_t)) {
+            fprintf(stderr, 
+                "Can't calloc %lu elements of size %lu at %s line %d\n", 
+                (unsigned long)count, (unsigned long)size, file, line);
+        }
+        else {
+            fprintf(stderr, "calloc failed at %s line %d\n", file, line);
+        }
+        exit(1);
+    }
+    return pointer;
+}
+
+void*
+CFCUtil_wrapped_realloc(void *ptr, size_t size, const char *file, int line)
+{
+    void *pointer = realloc(ptr, size);
+    if (pointer == NULL && size != 0) {
+        if (sizeof(long) >= sizeof(size_t)) {
+            fprintf(stderr, "Can't realloc %lu bytes at %s line %d\n", 
+                (unsigned long)size, file, line);
+        }
+        else {
+            fprintf(stderr, "realloc failed at %s line %d\n", file, line);
+        }
+        exit(1);
+    }
+    return pointer;
+}
+
+void
+CFCUtil_wrapped_free(void *ptr)
+{
+    free(ptr);
+}
+
+

Modified: incubator/lucy/trunk/clownfish/src/CFCUtil.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCUtil.h?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCUtil.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCUtil.h Wed Feb 23 23:56:07 2011
@@ -45,5 +45,39 @@ CFCUtil_strndup(const char *string, size
 void
 CFCUtil_trim_whitespace(char *text);
 
+/** Attempt to allocate memory with malloc, but print an error and exit if the
+ * call fails.
+ */
+void*
+CFCUtil_wrapped_malloc(size_t count, const char *file, int line);
+
+/** Attempt to allocate memory with calloc, but print an error and exit if the
+ * call fails.
+ */
+void*
+CFCUtil_wrapped_calloc(size_t count, size_t size, const char *file, int line);
+
+/** Attempt to allocate memory with realloc, but print an error and exit if 
+ * the call fails.
+ */
+void*
+CFCUtil_wrapped_realloc(void *ptr, size_t size, const char *file, int line);
+
+/** Free memory.  (Wrapping is necessary in cases where memory allocated
+ * within Clownfish has to be freed in an external environment where "free"
+ * may have been redefined.)
+ */
+void
+CFCUtil_wrapped_free(void *ptr);
+
+#define MALLOCATE(_count) \
+    CFCUtil_wrapped_malloc((_count), __FILE__, __LINE__)
+#define CALLOCATE(_count, _size) \
+    CFCUtil_wrapped_calloc((_count), (_size), __FILE__, __LINE__)
+#define REALLOCATE(_ptr, _count) \
+    CFCUtil_wrapped_realloc((_ptr), (_count), __FILE__, __LINE__)
+#define FREEMEM(_ptr) \
+    CFCUtil_wrapped_free(_ptr)
+
 #endif /* H_CFCUTIL */
 

Modified: incubator/lucy/trunk/clownfish/src/CFCVariable.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCVariable.c?rev=1073991&r1=1073990&r2=1073991&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCVariable.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCVariable.c Wed Feb 23 23:56:07 2011
@@ -79,21 +79,18 @@ CFCVariable_init(CFCVariable *self, stru
     {
         size_t size = strlen(type_str) + sizeof(" ") + strlen(micro_sym) +
             strlen(postfix) + 1;
-        self->local_c = (char*)malloc(size);
-        if (!self->local_c) { croak("malloc failed"); }
+        self->local_c = (char*)MALLOCATE(size);
         sprintf(self->local_c, "%s %s%s", type_str, micro_sym, postfix);
     }
     {
-        self->local_dec = (char*)malloc(strlen(self->local_c) + sizeof(";\0"));
-        if (!self->local_dec) { croak("malloc failed"); }
+        self->local_dec = (char*)MALLOCATE(strlen(self->local_c) + sizeof(";\0"));
         sprintf(self->local_dec, "%s;", self->local_c);
     }
     {
         const char *full_sym = CFCSymbol_full_sym((CFCSymbol*)self);
         size_t size = strlen(type_str) + sizeof(" ") + strlen(full_sym) +
             strlen(postfix) + 1;
-        self->global_c = (char*)malloc(size);
-        if (!self->global_c) { croak("malloc failed"); }
+        self->global_c = (char*)MALLOCATE(size);
         sprintf(self->global_c, "%s %s%s", type_str, full_sym, postfix);
     }
 
@@ -104,9 +101,9 @@ void
 CFCVariable_destroy(CFCVariable *self)
 {
     CFCBase_decref((CFCBase*)self->type);
-    free(self->local_c);
-    free(self->global_c);
-    free(self->local_dec);
+    FREEMEM(self->local_c);
+    FREEMEM(self->global_c);
+    FREEMEM(self->local_dec);
     CFCSymbol_destroy((CFCSymbol*)self);
 }