Asterisk - The Open Source Telephony Project  GIT-master-a24979a
Functions
bt_close.c File Reference
#include <sys/param.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../include/db.h"
#include "btree.h"
Include dependency graph for bt_close.c:

Go to the source code of this file.

Functions

int __bt_close (DB *dbp)
 
int __bt_sync (DB *dbp, u_int flags) const
 
static int bt_meta __P ((BTREE *))
 
static int bt_meta (BTREE *t)
 

Function Documentation

◆ __bt_close()

int __bt_close ( DB dbp)

Definition at line 64 of file bt_close.c.

66 {
67  BTREE *t;
68  int fd;
69 
70  t = dbp->internal;
71 
72  /* Toss any page pinned across calls. */
73  if (t->bt_pinned != NULL) {
74  mpool_put(t->bt_mp, t->bt_pinned, 0);
75  t->bt_pinned = NULL;
76  }
77 
78  /* Sync the tree. */
79  if (__bt_sync(dbp, 0) == RET_ERROR)
80  return (RET_ERROR);
81 
82  /* Close the memory pool. */
83  if (mpool_close(t->bt_mp) == RET_ERROR)
84  return (RET_ERROR);
85 
86  /* Free random memory. */
87  if (t->bt_cursor.key.data != NULL) {
88  free(t->bt_cursor.key.data);
89  t->bt_cursor.key.size = 0;
90  t->bt_cursor.key.data = NULL;
91  }
92  if (t->bt_rkey.data) {
93  free(t->bt_rkey.data);
94  t->bt_rkey.size = 0;
95  t->bt_rkey.data = NULL;
96  }
97  if (t->bt_rdata.data) {
98  free(t->bt_rdata.data);
99  t->bt_rdata.size = 0;
100  t->bt_rdata.data = NULL;
101  }
102 
103  fd = t->bt_fd;
104  free(t);
105  free(dbp);
106  return (close(fd) ? RET_ERROR : RET_SUCCESS);
107 }
int __bt_sync(DB *dbp, u_int flags) const
Definition: bt_close.c:119
#define RET_SUCCESS
Definition: db.h:52
#define RET_ERROR
Definition: db.h:51
void free()
static DB * dbp
Definition: hsearch.c:49
int mpool_close(MPOOL *mp)
Definition: mpool.c:279
int mpool_put(MPOOL *mp, void *page, u_int flags)
Definition: mpool.c:251
#define NULL
Definition: resample.c:96
void * data
Definition: db.h:86
size_t size
Definition: db.h:87
void * internal
Definition: db.h:137
Definition: btree.h:312
CURSOR bt_cursor
Definition: btree.h:320
int bt_fd
Definition: btree.h:335
DBT bt_rkey
Definition: btree.h:332
MPOOL * bt_mp
Definition: btree.h:313
PAGE * bt_pinned
Definition: btree.h:318
DBT bt_rdata
Definition: btree.h:333
DBT key
Definition: btree.h:285

References __bt_sync(), _btree::bt_cursor, _btree::bt_fd, _btree::bt_mp, _btree::bt_pinned, _btree::bt_rdata, _btree::bt_rkey, DBT::data, dbp, free(), __db::internal, _cursor::key, mpool_close(), mpool_put(), NULL, RET_ERROR, RET_SUCCESS, and DBT::size.

Referenced by __bt_open(), __rec_close(), and __rec_open().

◆ __bt_sync()

int __bt_sync ( DB dbp,
u_int  flags 
) const

Definition at line 119 of file bt_close.c.

122 {
123  BTREE *t;
124  int status;
125 
126  t = dbp->internal;
127 
128  /* Toss any page pinned across calls. */
129  if (t->bt_pinned != NULL) {
130  mpool_put(t->bt_mp, t->bt_pinned, 0);
131  t->bt_pinned = NULL;
132  }
133 
134  /* Sync doesn't currently take any flags. */
135  if (flags != 0) {
136  errno = EINVAL;
137  return (RET_ERROR);
138  }
139 
140  if (F_ISSET(t, B_INMEM | B_RDONLY) || !F_ISSET(t, B_MODIFIED))
141  return (RET_SUCCESS);
142 
143  if (F_ISSET(t, B_METADIRTY) && bt_meta(t) == RET_ERROR)
144  return (RET_ERROR);
145 
146  if ((status = mpool_sync(t->bt_mp)) == RET_SUCCESS)
147  F_CLR(t, B_MODIFIED);
148 
149  return (status);
150 }
jack_status_t status
Definition: app_jack.c:146
static int bt_meta(BTREE *t)
Definition: bt_close.c:162
#define F_ISSET(p, f)
Definition: btree.h:42
#define B_MODIFIED
Definition: btree.h:370
#define B_METADIRTY
Definition: btree.h:369
#define B_RDONLY
Definition: btree.h:372
#define F_CLR(p, f)
Definition: btree.h:41
#define B_INMEM
Definition: btree.h:368
int errno
int mpool_sync(MPOOL *mp)
Definition: mpool.c:300

References B_INMEM, B_METADIRTY, B_MODIFIED, B_RDONLY, bt_meta(), _btree::bt_mp, _btree::bt_pinned, dbp, errno, F_CLR, F_ISSET, __db::internal, mpool_put(), mpool_sync(), NULL, RET_ERROR, RET_SUCCESS, and status.

Referenced by __bt_close(), __bt_open(), and __rec_sync().

◆ __P()

static int bt_meta __P ( (BTREE *)  )
static

◆ bt_meta()

static int bt_meta ( BTREE t)
static

Definition at line 162 of file bt_close.c.

164 {
165  BTMETA m;
166  void *p;
167 
168  if ((p = mpool_get(t->bt_mp, P_META, 0)) == NULL)
169  return (RET_ERROR);
170 
171  /* Fill in metadata. */
172  m.magic = BTREEMAGIC;
173  m.version = BTREEVERSION;
174  m.psize = t->bt_psize;
175  m.free = t->bt_free;
176  m.nrecs = t->bt_nrecs;
177  m.flags = F_ISSET(t, SAVEMETA);
178 
179  memmove(p, &m, sizeof(BTMETA));
180  mpool_put(t->bt_mp, p, MPOOL_DIRTY);
181  return (RET_SUCCESS);
182 }
#define P_META
Definition: btree.h:64
#define SAVEMETA
Definition: btree.h:307
#define BTREEVERSION
Definition: db.h:142
#define BTREEMAGIC
Definition: db.h:141
void * mpool_get(MPOOL *mp, pgno_t pgno, u_int flags)
Definition: mpool.c:165
#define MPOOL_DIRTY
Definition: mpool.h:61
Definition: btree.h:300
u_int32_t flags
Definition: btree.h:308
u_int32_t magic
Definition: btree.h:301
u_int32_t nrecs
Definition: btree.h:305
u_int32_t free
Definition: btree.h:304
u_int32_t version
Definition: btree.h:302
u_int32_t psize
Definition: btree.h:303
u_int32_t bt_psize
Definition: btree.h:338
pgno_t bt_free
Definition: btree.h:337
recno_t bt_nrecs
Definition: btree.h:360

References _btree::bt_free, _btree::bt_mp, _btree::bt_nrecs, _btree::bt_psize, BTREEMAGIC, BTREEVERSION, F_ISSET, _btmeta::flags, _btmeta::free, _btmeta::magic, MPOOL_DIRTY, mpool_get(), mpool_put(), _btmeta::nrecs, NULL, P_META, _btmeta::psize, RET_ERROR, RET_SUCCESS, SAVEMETA, and _btmeta::version.

Referenced by __bt_sync().