GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
new.c
Go to the documentation of this file.
1 /*
2  ** Written by David Gerdes US Army Construction Engineering Research Lab
3  ** April 1992
4  ** Copyright 1992 USA-CERL All rights reserved.
5  **
6  */
7 #include <string.h>
8 #include <stdlib.h>
9 #include <grass/linkm.h>
10 
11 
12 struct link_head *link_new(struct link_head *Head)
13 {
14  VOID_T *tmp;
15  char *ctmp, *p;
16  register int i;
17 
18  if (Head->Unused == NULL) {
19  if (Head->max_ptr >= Head->alloced) {
20  /*DEBUG fprintf (stderr, "REALLOCING PTR_ARRAY (%d -> %d)\n", Head->alloced, Head->alloced * 2); */
21  if (NULL ==
22  (tmp =
23  (VOID_T *) realloc(Head->ptr_array,
24  sizeof(VOID_T *) * Head->alloced * 2))) {
25  if (Head->exit_flag)
27  return NULL;
28  }
29  Head->ptr_array = (VOID_T **) tmp;
30  Head->alloced *= 2;
31  }
32 
33  /*DEBUG fprintf (stderr, "Mallocing another chunk: %d\n", Head->max_ptr); */
34  if (NULL == (tmp = (VOID_T *)
35  malloc(Head->chunk_size * Head->unit_size))) {
36  if (Head->exit_flag)
38  return (struct link_head *)NULL;
39  }
40 
41  Head->ptr_array[Head->max_ptr++] = (VOID_T *) tmp;
42  Head->Unused = (VOID_T *) tmp;
43 
44  p = ctmp = (char *)tmp;
45  for (i = 0; i < Head->chunk_size - 1; i++) {
46  link__set_next((VOID_T *) p,
47  (VOID_T *) & (ctmp[(i + 1) * Head->unit_size]));
48  /* p->next = p+1 */
49 
50  p = &(ctmp[(i + 1) * Head->unit_size]); /* p = p->next */
51  }
52  link__set_next((VOID_T *) p, NULL); /* p->next = NULL */
53  }
54 
55  tmp = Head->Unused;
56 
57  /* Unused = Unused->next */
58  Head->Unused = link__get_next(Head->Unused);
59 
60  return (struct link_head *)tmp;
61 }
#define VOID_T
Definition: qtree.h:17
struct link_head * link_new(struct link_head *Head)
Definition: new.c:12
void * malloc(YYSIZE_T)
int link_out_of_memory(void)
Definition: oom.c:4
void link__set_next(VOID_T *a, VOID_T *b)
Definition: linkm/next.c:18
VOID_T * link__get_next(VOID_T *list)
Definition: linkm/next.c:9
return NULL
Definition: dbfopen.c:1394