Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / stdlib / malloc / phys.h
1 /* $Id: phys.h,v 1.4 1994/06/24 11:55:38 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*      Algorithms to manipulate the doubly-linked list of physical
7         chunks.
8 */
9 publicdata mallink *ml_last;
10
11 #define FREE_BIT                01
12 #ifdef STORE
13 #define STORE_BIT               02
14 #define BITS                    (FREE_BIT|STORE_BIT)
15 #else
16 #define BITS                    (FREE_BIT)
17 #endif
18
19 #define __bits(ml)              ((int)((size_type)_phys_prev_of(ml) & BITS))
20 #define __free_of(ml)           ((int)((size_type)_phys_prev_of(ml) & FREE_BIT))
21 #define __phys_prev_of(ml)      ((mallink *)((size_type)_phys_prev_of(ml) & ~BITS))
22 #define prev_size_of(ml)        ((char *)(ml) - \
23                                  (char *)__phys_prev_of(ml) - \
24                                  mallink_size() \
25                                 )
26 #define set_phys_prev(ml,e) \
27         (_phys_prev_of(ml) = (mallink *) ((char *)e + __bits(ml)))
28
29 #ifdef  CHECK
30 public Error(const char *fmt, const char *s, mallink *ml);
31 #define phys_prev_of(ml)        (mallink *) \
32         (first_mallink(ml) ? \
33                 (char *)Error("phys_prev_of first_mallink %p", "somewhere", ml) : \
34                 (char *)__phys_prev_of(ml) \
35         )
36 #else   /* ndef CHECK */
37 #define phys_prev_of(ml)        __phys_prev_of(ml)
38 #endif  /* CHECK */
39
40 #define first_mallink(ml)       (int) (__phys_prev_of(ml) == 0)
41 #define last_mallink(ml)        (int) ((ml) == ml_last)
42
43 /*      There is an ambiguity in the semantics of phys_next_of: sometimes
44         one wants it to return MAL_NULL if there is no next chunk, at
45         other times one wants the address of the virtual chunk at the
46         end of memory.  The present version returns the address of the
47         (virtual) chunk and relies on the user to test last_mallink(ml)
48         first.
49 */
50 #define size_of(ml)             (_this_size_of(ml) - mallink_size())
51 #define set_phys_next(ml,e) \
52         (_this_size_of(ml) = (size_type)((char *)(e) - (char *)(ml)))
53 #define phys_next_of(ml)        (mallink *) ((char *)(ml) + _this_size_of(ml))
54
55 #define set_free(ml,e) \
56         (_phys_prev_of(ml) = (mallink *) \
57                 ((e) ? (size_type) _phys_prev_of(ml) | FREE_BIT : \
58                        (size_type) _phys_prev_of(ml) & ~FREE_BIT))
59 #define free_of(ml)             (__free_of(ml))
60
61 #define coalesce_forw(ml,nxt)   ( unlink_free_chunk(nxt), \
62                                   combine_chunks((ml), (nxt)))
63
64 #define coalesce_backw(ml,prv)  ( unlink_free_chunk(prv), \
65                                   stopped_working_on(ml), \
66                                   combine_chunks((prv), (ml)), \
67                                   started_working_on(prv))
68
69 #ifdef  CHECK
70 #define set_print(ml,e)         (_print_of(ml) = (e))
71 #define print_of(ml)            (_print_of(ml))
72 #endif  /* CHECK */
73
74 public truncate(mallink *ml, size_t size);
75 public combine_chunks(register mallink *ml1, register mallink *ml2);
76 public mallink *create_chunk(void *p, size_t n);