Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions
hash_buf.c File Reference
#include <sys/param.h>
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include "../include/db.h"
#include "hash.h"
#include "page.h"
#include "extern.h"
Include dependency graph for hash_buf.c:

Go to the source code of this file.

Macros

#define BUF_INSERT(B, P)
 
#define BUF_REMOVE(B)
 
#define LRU   hashp->bufhead.prev
 
#define LRU_INSERT(B)   BUF_INSERT((B), LRU)
 
#define MRU   hashp->bufhead.next
 
#define MRU_INSERT(B)   BUF_INSERT((B), &hashp->bufhead)
 

Functions

int __buf_free (HTAB *hashp, int do_free, int to_disk)
 
void __buf_init (HTAB *hashp, int nbytes)
 
BUFHEAD__get_buf (HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp, int newpage)
 
static BUFHEAD *newbuf __P ((HTAB *, u_int32_t, BUFHEAD *))
 
void __reclaim_buf (HTAB *hashp, BUFHEAD *bp)
 
static BUFHEADnewbuf (HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp)
 

Macro Definition Documentation

◆ BUF_INSERT

#define BUF_INSERT (   B,
  P 
)

Definition at line 82 of file hash_buf.c.

◆ BUF_REMOVE

#define BUF_REMOVE (   B)
Value:
{ \
(B)->prev->next = (B)->next; \
(B)->next->prev = (B)->prev; \
}

Definition at line 76 of file hash_buf.c.

Referenced by __buf_free(), __get_buf(), __reclaim_buf(), and newbuf().

◆ LRU

#define LRU   hashp->bufhead.prev

Definition at line 90 of file hash_buf.c.

Referenced by __buf_free(), and newbuf().

◆ LRU_INSERT

#define LRU_INSERT (   B)    BUF_INSERT((B), LRU)

Definition at line 93 of file hash_buf.c.

Referenced by __reclaim_buf(), and newbuf().

◆ MRU

#define MRU   hashp->bufhead.next

Definition at line 89 of file hash_buf.c.

◆ MRU_INSERT

#define MRU_INSERT (   B)    BUF_INSERT((B), &hashp->bufhead)

Definition at line 92 of file hash_buf.c.

Referenced by __get_buf(), and newbuf().

Function Documentation

◆ __buf_free()

int __buf_free ( HTAB hashp,
int  do_free,
int  to_disk 
)

Definition at line 315 of file hash_buf.c.

References __put_page(), _bufhead::addr, BUF_MOD, BUF_REMOVE, htab::bufhead, _bufhead::flags, free(), IS_BUCKET, LRU, _bufhead::page, and _bufhead::prev.

Referenced by hash_sync(), and hdestroy().

318 {
319  BUFHEAD *bp;
320 
321  /* Need to make sure that buffer manager has been initialized */
322  if (!LRU)
323  return (0);
324  for (bp = LRU; bp != &hashp->bufhead;) {
325  /* Check that the buffer is valid */
326  if (bp->addr || IS_BUCKET(bp->flags)) {
327  if (to_disk && (bp->flags & BUF_MOD) &&
328  __put_page(hashp, bp->page,
329  bp->addr, IS_BUCKET(bp->flags), 0))
330  return (-1);
331  }
332  /* Check if we are freeing stuff */
333  if (do_free) {
334  if (bp->page)
335  free(bp->page);
336  BUF_REMOVE(bp);
337  free(bp);
338  bp = LRU;
339  } else
340  bp = bp->prev;
341  }
342  return (0);
343 }
u_int32_t addr
Definition: hash.h:51
#define LRU
Definition: hash_buf.c:90
char * page
Definition: hash.h:52
void free()
#define BUF_MOD
Definition: hash.h:54
int __put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
Definition: hash_page.c:578
#define BUF_REMOVE(B)
Definition: hash_buf.c:76
Definition: hash.h:47
BUFHEAD * prev
Definition: hash.h:48
BUFHEAD bufhead
Definition: hash.h:117
char flags
Definition: hash.h:53
#define IS_BUCKET(X)
Definition: hash.h:60

◆ __buf_init()

void __buf_init ( HTAB hashp,
int  nbytes 
)

Definition at line 290 of file hash_buf.c.

References htab::bufhead, MAX, MIN_BUFFERS, htab::nbufs, _bufhead::next, and _bufhead::prev.

Referenced by __hash_open().

293 {
294  BUFHEAD *bfp;
295  int npages;
296 
297  bfp = &(hashp->bufhead);
298  npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
299  npages = MAX(npages, MIN_BUFFERS);
300 
301  hashp->nbufs = npages;
302  bfp->next = bfp;
303  bfp->prev = bfp;
304  /*
305  * This space is calloc'd so these are already null.
306  *
307  * bfp->ovfl = NULL;
308  * bfp->flags = 0;
309  * bfp->page = NULL;
310  * bfp->addr = 0;
311  */
312 }
#define MIN_BUFFERS
Definition: hash.h:125
BUFHEAD * next
Definition: hash.h:49
#define MAX(a, b)
Definition: utils.h:228
Definition: hash.h:47
BUFHEAD * prev
Definition: hash.h:48
BUFHEAD bufhead
Definition: hash.h:117
int nbufs
Definition: hash.h:115

◆ __get_buf()

BUFHEAD* __get_buf ( HTAB hashp,
u_int32_t  addr,
BUFHEAD prev_bp,
int  newpage 
)

Definition at line 105 of file hash_buf.c.

References __get_page(), _bufhead::addr, BUF_DISK, BUF_REMOVE, htab::dir, ISDISK, MRU_INSERT, htab::new_file, newbuf(), NULL, _bufhead::ovfl, _bufhead::page, and PTROF.

Referenced by __add_ovflpage(), __addel(), __big_delete(), __big_return(), __big_split(), __find_bigpair(), __find_last_page(), __split_page(), collect_data(), collect_key(), fetch_bitmap(), hash_access(), hash_seq(), and ugly_split().

110 {
111  register BUFHEAD *bp;
112  register u_int32_t is_disk_mask;
113  register int is_disk, segment_ndx = 0;
114  SEGMENT segp = 0;
115 
116  is_disk = 0;
117  is_disk_mask = 0;
118  if (prev_bp) {
119  bp = prev_bp->ovfl;
120  if (!bp || (bp->addr != addr))
121  bp = NULL;
122  if (!newpage)
123  is_disk = BUF_DISK;
124  } else {
125  /* Grab buffer out of directory */
126  segment_ndx = addr & (hashp->SGSIZE - 1);
127 
128  /* valid segment ensured by __call_hash() */
129  segp = hashp->dir[addr >> hashp->SSHIFT];
130 #ifdef DEBUG
131  assert(segp != NULL);
132 #endif
133  bp = PTROF(segp[segment_ndx]);
134  is_disk_mask = ISDISK(segp[segment_ndx]);
135  is_disk = is_disk_mask || !hashp->new_file;
136  }
137 
138  if (!bp) {
139  bp = newbuf(hashp, addr, prev_bp);
140  if (!bp ||
141  __get_page(hashp, bp->page, addr, !prev_bp, is_disk, 0))
142  return (NULL);
143  if (!prev_bp)
144  segp[segment_ndx] =
145  (BUFHEAD *)((ptrdiff_t)bp | is_disk_mask);
146  } else {
147  BUF_REMOVE(bp);
148  MRU_INSERT(bp);
149  }
150  return (bp);
151 }
u_int32_t addr
Definition: hash.h:51
#define ISDISK(X)
Definition: hash.h:147
BUFHEAD * ovfl
Definition: hash.h:50
int new_file
Definition: hash.h:108
#define NULL
Definition: resample.c:96
char * page
Definition: hash.h:52
#define BUF_REMOVE(B)
Definition: hash_buf.c:76
Definition: hash.h:47
#define PTROF(X)
Definition: hash.h:144
SEGMENT * dir
Definition: hash.h:118
#define BUF_DISK
Definition: hash.h:55
unsigned int u_int32_t
int __get_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_disk, int is_bitmap)
Definition: hash_page.c:518
#define MRU_INSERT(B)
Definition: hash_buf.c:92
static BUFHEAD * newbuf(HTAB *hashp, u_int32_t addr, BUFHEAD *prev_bp)
Definition: hash_buf.c:160

◆ __P()

static BUFHEAD* newbuf __P ( (HTAB *, u_int32_t, BUFHEAD *)  )
static

◆ __reclaim_buf()

void __reclaim_buf ( HTAB hashp,
BUFHEAD bp 
)

Definition at line 346 of file hash_buf.c.

References _bufhead::addr, BUF_REMOVE, _bufhead::flags, LRU_INSERT, and _bufhead::ovfl.

Referenced by __free_ovflpage().

349 {
350  bp->ovfl = 0;
351  bp->addr = 0;
352  bp->flags = 0;
353  BUF_REMOVE(bp);
354  LRU_INSERT(bp);
355 }
u_int32_t addr
Definition: hash.h:51
BUFHEAD * ovfl
Definition: hash.h:50
#define BUF_REMOVE(B)
Definition: hash_buf.c:76
#define LRU_INSERT(B)
Definition: hash_buf.c:93
char flags
Definition: hash.h:53

◆ newbuf()

static BUFHEAD* newbuf ( HTAB hashp,
u_int32_t  addr,
BUFHEAD prev_bp 
)
static

Definition at line 160 of file hash_buf.c.

References __put_page(), _bufhead::addr, BUF_BUCKET, BUF_DISK, BUF_MOD, BUF_PIN, BUF_REMOVE, htab::dir, _bufhead::flags, free(), if(), IS_BUCKET, ISDISK, LRU, LRU_INSERT, malloc(), MRU_INSERT, htab::nbufs, htab::new_file, NULL, _bufhead::ovfl, and _bufhead::page.

Referenced by __get_buf(), and xmpp_client_receive().

164 {
165  register BUFHEAD *bp; /* The buffer we're going to use */
166  register BUFHEAD *xbp; /* Temp pointer */
167  register BUFHEAD *next_xbp;
168  SEGMENT segp;
169  int segment_ndx;
170  u_int16_t oaddr, *shortp;
171 
172  oaddr = 0;
173  bp = LRU;
174  /*
175  * If LRU buffer is pinned, the buffer pool is too small. We need to
176  * allocate more buffers.
177  */
178  if (hashp->nbufs || (bp->flags & BUF_PIN)) {
179  /* Allocate a new one */
180  if ((bp = (BUFHEAD *)malloc(sizeof(BUFHEAD))) == NULL)
181  return (NULL);
182 #ifdef PURIFY
183  memset(bp, 0xff, sizeof(BUFHEAD));
184 #endif
185  if ((bp->page = (char *)malloc(hashp->BSIZE)) == NULL) {
186  free(bp);
187  return (NULL);
188  }
189 #ifdef PURIFY
190  memset(bp->page, 0xff, hashp->BSIZE);
191 #endif
192  if (hashp->nbufs)
193  hashp->nbufs--;
194  } else {
195  /* Kick someone out */
196  BUF_REMOVE(bp);
197  /*
198  * If this is an overflow page with addr 0, it's already been
199  * flushed back in an overflow chain and initialized.
200  */
201  if ((bp->addr != 0) || (bp->flags & BUF_BUCKET)) {
202  /*
203  * Set oaddr before __put_page so that you get it
204  * before bytes are swapped.
205  */
206  shortp = (u_int16_t *)bp->page;
207  if (shortp[0])
208  oaddr = shortp[shortp[0] - 1];
209  if ((bp->flags & BUF_MOD) && __put_page(hashp, bp->page,
210  bp->addr, (int)IS_BUCKET(bp->flags), 0))
211  return (NULL);
212  /*
213  * Update the pointer to this page (i.e. invalidate it).
214  *
215  * If this is a new file (i.e. we created it at open
216  * time), make sure that we mark pages which have been
217  * written to disk so we retrieve them from disk later,
218  * rather than allocating new pages.
219  */
220  if (IS_BUCKET(bp->flags)) {
221  segment_ndx = bp->addr & (hashp->SGSIZE - 1);
222  segp = hashp->dir[bp->addr >> hashp->SSHIFT];
223 #ifdef DEBUG
224  assert(segp != NULL);
225 #endif
226 
227  if (hashp->new_file &&
228  ((bp->flags & BUF_MOD) ||
229  ISDISK(segp[segment_ndx])))
230  segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
231  else
232  segp[segment_ndx] = NULL;
233  }
234  /*
235  * Since overflow pages can only be access by means of
236  * their bucket, free overflow pages associated with
237  * this bucket.
238  */
239  for (xbp = bp; xbp->ovfl;) {
240  next_xbp = xbp->ovfl;
241  xbp->ovfl = 0;
242  xbp = next_xbp;
243 
244  /* Check that ovfl pointer is up date. */
245  if (IS_BUCKET(xbp->flags) ||
246  (oaddr != xbp->addr))
247  break;
248 
249  shortp = (u_int16_t *)xbp->page;
250  if (shortp[0])
251  /* set before __put_page */
252  oaddr = shortp[shortp[0] - 1];
253  if ((xbp->flags & BUF_MOD) && __put_page(hashp,
254  xbp->page, xbp->addr, 0, 0))
255  return (NULL);
256  xbp->addr = 0;
257  xbp->flags = 0;
258  BUF_REMOVE(xbp);
259  LRU_INSERT(xbp);
260  }
261  }
262  }
263 
264  /* Now assign this buffer */
265  bp->addr = addr;
266 #ifdef DEBUG1
267  (void)fprintf(stderr, "NEWBUF1: %d->ovfl was %d is now %d\n",
268  bp->addr, (bp->ovfl ? bp->ovfl->addr : 0), 0);
269 #endif
270  bp->ovfl = NULL;
271  if (prev_bp) {
272  /*
273  * If prev_bp is set, this is an overflow page, hook it in to
274  * the buffer overflow links.
275  */
276 #ifdef DEBUG1
277  (void)fprintf(stderr, "NEWBUF2: %d->ovfl was %d is now %d\n",
278  prev_bp->addr, (prev_bp->ovfl ? bp->ovfl->addr : 0),
279  (bp ? bp->addr : 0));
280 #endif
281  prev_bp->ovfl = bp;
282  bp->flags = 0;
283  } else
284  bp->flags = BUF_BUCKET;
285  MRU_INSERT(bp);
286  return (bp);
287 }
u_int32_t addr
Definition: hash.h:51
#define ISDISK(X)
Definition: hash.h:147
BUFHEAD * ovfl
Definition: hash.h:50
if(!yyg->yy_init)
Definition: ast_expr2f.c:868
int new_file
Definition: hash.h:108
#define NULL
Definition: resample.c:96
#define LRU
Definition: hash_buf.c:90
char * page
Definition: hash.h:52
char * malloc()
void free()
#define BUF_MOD
Definition: hash.h:54
int __put_page(HTAB *hashp, char *p, u_int32_t bucket, int is_bucket, int is_bitmap)
Definition: hash_page.c:578
#define BUF_REMOVE(B)
Definition: hash_buf.c:76
Definition: hash.h:47
SEGMENT * dir
Definition: hash.h:118
#define BUF_BUCKET
Definition: hash.h:56
unsigned short u_int16_t
#define LRU_INSERT(B)
Definition: hash_buf.c:93
char flags
Definition: hash.h:53
#define BUF_DISK
Definition: hash.h:55
#define IS_BUCKET(X)
Definition: hash.h:60
#define BUF_PIN
Definition: hash.h:57
int nbufs
Definition: hash.h:115
#define MRU_INSERT(B)
Definition: hash_buf.c:92