GRASS GIS 7 Programmer's Manual
7.9.dev(2021)-e5379bbd7
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
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", 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
)
26
link_out_of_memory
();
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
)
37
link_out_of_memory
();
38
return
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
tmp;
61
}
link_head::Unused
VOID_T * Unused
Definition:
linkm.h:19
VOID_T
#define VOID_T
Definition:
linkm.h:8
link_head
Definition:
linkm.h:12
link_new
VOID_T * link_new(struct link_head *Head)
Definition:
new.c:12
link_head::exit_flag
int exit_flag
Definition:
linkm.h:20
link_out_of_memory
int link_out_of_memory(void)
Definition:
oom.c:4
link__set_next
void link__set_next(VOID_T *, VOID_T *)
Definition:
linkm/next.c:18
NULL
#define NULL
Definition:
ccmath.h:32
malloc
void * malloc(YYSIZE_T)
link_head::chunk_size
int chunk_size
Definition:
linkm.h:17
link_head::unit_size
int unit_size
Definition:
linkm.h:18
link_head::alloced
int alloced
Definition:
linkm.h:16
linkm.h
link_head::ptr_array
VOID_T ** ptr_array
Definition:
linkm.h:14
link_head::max_ptr
int max_ptr
Definition:
linkm.h:15
link__get_next
VOID_T * link__get_next(VOID_T *)
Definition:
linkm/next.c:9
lib
linkm
new.c
Generated on Mon May 31 2021 05:21:30 for GRASS GIS 7 Programmer's Manual by
1.8.13