GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-2653d7b383
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 
8 #include <string.h>
9 #include <stdlib.h>
10 #include <grass/linkm.h>
11 
12 VOID_T *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",
21  * Head->alloced, Head->alloced * 2); */
22  if (NULL ==
23  (tmp = (VOID_T *)realloc(
24  Head->ptr_array, 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",
34  * Head->max_ptr); */
35  if (NULL ==
36  (tmp = (VOID_T *)malloc(Head->chunk_size * Head->unit_size))) {
37  if (Head->exit_flag)
39  return NULL;
40  }
41 
42  Head->ptr_array[Head->max_ptr++] = (VOID_T *)tmp;
43  Head->Unused = (VOID_T *)tmp;
44 
45  p = ctmp = (char *)tmp;
46  for (i = 0; i < Head->chunk_size - 1; i++) {
48  (VOID_T *)&(ctmp[(i + 1) * Head->unit_size]));
49  /* p->next = p+1 */
50 
51  p = &(ctmp[(i + 1) * Head->unit_size]); /* p = p->next */
52  }
53  link__set_next((VOID_T *)p, NULL); /* p->next = NULL */
54  }
55 
56  tmp = Head->Unused;
57 
58  /* Unused = Unused->next */
59  Head->Unused = link__get_next(Head->Unused);
60 
61  return tmp;
62 }
#define NULL
Definition: ccmath.h:32
int link_out_of_memory(void)
Definition: oom.c:4
VOID_T * link__get_next(VOID_T *)
Definition: linkm/next.c:10
void link__set_next(VOID_T *, VOID_T *)
Definition: linkm/next.c:19
#define VOID_T
Definition: linkm.h:8
VOID_T * link_new(struct link_head *Head)
Definition: new.c:12
void * malloc(YYSIZE_T)