GRASS Programmer's Manual
6.4.2(2012)
|
00001 /* 00002 ** Written by David Gerdes US Army Construction Engineering Research Lab 00003 ** April 1992 00004 ** Copyright 1992 USA-CERL All rights reserved. 00005 ** 00006 */ 00007 #include <string.h> 00008 #include <stdlib.h> 00009 #include <grass/linkm.h> 00010 00011 00012 struct link_head *link_new(struct link_head *Head) 00013 { 00014 VOID_T *tmp; 00015 char *ctmp, *p; 00016 register int i; 00017 00018 if (Head->Unused == NULL) { 00019 if (Head->max_ptr >= Head->alloced) { 00020 /*DEBUG fprintf (stderr, "REALLOCING PTR_ARRAY (%d -> %d)\n", Head->alloced, Head->alloced * 2); */ 00021 if (NULL == 00022 (tmp = 00023 (VOID_T *) realloc(Head->ptr_array, 00024 sizeof(VOID_T *) * Head->alloced * 2))) { 00025 if (Head->exit_flag) 00026 link_out_of_memory(); 00027 return NULL; 00028 } 00029 Head->ptr_array = (VOID_T **) tmp; 00030 Head->alloced *= 2; 00031 } 00032 00033 /*DEBUG fprintf (stderr, "Mallocing another chunk: %d\n", Head->max_ptr); */ 00034 if (NULL == (tmp = (VOID_T *) 00035 malloc(Head->chunk_size * Head->unit_size))) { 00036 if (Head->exit_flag) 00037 link_out_of_memory(); 00038 return (struct link_head *)NULL; 00039 } 00040 00041 Head->ptr_array[Head->max_ptr++] = (VOID_T *) tmp; 00042 Head->Unused = (VOID_T *) tmp; 00043 00044 p = ctmp = (char *)tmp; 00045 for (i = 0; i < Head->chunk_size - 1; i++) { 00046 link__set_next((VOID_T *) p, 00047 (VOID_T *) & (ctmp[(i + 1) * Head->unit_size])); 00048 /* p->next = p+1 */ 00049 00050 p = &(ctmp[(i + 1) * Head->unit_size]); /* p = p->next */ 00051 } 00052 link__set_next((VOID_T *) p, NULL); /* p->next = NULL */ 00053 } 00054 00055 tmp = Head->Unused; 00056 00057 /* Unused = Unused->next */ 00058 Head->Unused = link__get_next(Head->Unused); 00059 00060 return (struct link_head *)tmp; 00061 }