8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-29 06:43:03 +01:00
firebird-mirror/src/jrd/all.cpp

391 lines
9.2 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: JRD Access Method
* MODULE: all.c
* DESCRIPTION: Internal block allocator
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*/
#ifdef SHLIB_DEFS
#define LOCAL_SHLIB_DEFS
#endif
#include "firebird.h"
2001-05-23 15:26:42 +02:00
#include <string.h>
#include "../jrd/ib_stdio.h"
#include "gen/codes.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/everything.h"
#include "../jrd/all_proto.h"
#include "../jrd/err_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/mov_proto.h"
#include "../jrd/thd_proto.h"
2003-01-20 12:10:30 +01:00
#include <algorithm>
2001-05-23 15:26:42 +02:00
#ifdef SHLIB_DEFS
#define strlen (*_libgds_strlen)
extern int strlen();
#endif
2001-12-24 03:51:06 +01:00
#define PERM_EXTEND_SIZE (16 * 1024)
#define CACH_EXTEND_SIZE (16 * 1024)
2001-05-23 15:26:42 +02:00
#ifdef DEV_BUILD
2001-12-24 03:51:06 +01:00
void ALL_check_memory()
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* A L L _ c h e c k _ m e m o r y
*
**************************************
*
* Functional description
* This routine goes through all allocated
* memory pools and checks the hunks of
* memory allocated to make sure they are
* valid. It is designed to be used for
* debugging purposes to find bad memory
* areas as soon as they are corrupted.
* A call to this routine can be made from
* looper() to ensure that it will be regularly
* executed.
*
**************************************/
2001-12-24 03:51:06 +01:00
Firebird::vector<JrdMemoryPool*>::iterator itr;
2001-05-23 15:26:42 +02:00
2001-12-24 03:51:06 +01:00
DBB Dbb = GET_DBB;
2001-05-23 15:26:42 +02:00
#ifdef V4_THREADING
2001-12-24 03:51:06 +01:00
V4_RW_LOCK_LOCK(dbb->dbb_rw_locks + DBB_WLCK_pools, WLCK_read);
#endif
2001-05-23 15:26:42 +02:00
2001-12-24 03:51:06 +01:00
// walk through all the pools in the database
for (itr = Dbb->dbb_pools.begin(); itr < Dbb->dbb_pools.end(); ++itr)
{
JrdMemoryPool* pool = *itr;
if (pool) {
// walk through all the hunks in the pool
pool->verify_pool();
}
2001-05-23 15:26:42 +02:00
}
#ifdef V4_THREADING
2001-05-23 15:26:42 +02:00
V4_RW_LOCK_UNLOCK(dbb->dbb_rw_locks + DBB_WLCK_pools);
#endif
2001-05-23 15:26:42 +02:00
}
#endif /* DEV_BUILD */
JrdMemoryPool *JrdMemoryPool::createPool(int *cur_mem, int *max_mem) {
2003-01-20 12:10:30 +01:00
DBB dbb = GET_DBB;
JrdMemoryPool *result = (JrdMemoryPool *)internal_create(sizeof(JrdMemoryPool),
cur_mem, max_mem);
result->plb_buckets = NULL;
result->plb_segments = NULL;
result->plb_dccs = NULL;
new (&result->lls_cache) BlockCache<lls> (*result);
2003-01-20 12:10:30 +01:00
if (dbb) dbb->dbb_pools.push_back(result);
return result;
}
JrdMemoryPool *JrdMemoryPool::createPool() {
DBB dbb = GET_DBB;
2003-01-20 12:10:30 +01:00
#ifdef SUPERSERVER
JrdMemoryPool *result = (JrdMemoryPool *)internal_create(sizeof(JrdMemoryPool),
(int*)&dbb->dbb_current_memory, (int*)&dbb->dbb_max_memory);
#else
JrdMemoryPool *result = (JrdMemoryPool *)internal_create(sizeof(JrdMemoryPool));
#endif
result->plb_buckets = NULL;
result->plb_segments = NULL;
result->plb_dccs = NULL;
new (&result->lls_cache) BlockCache<lls> (*result);
2003-01-20 12:10:30 +01:00
if (dbb) dbb->dbb_pools.push_back(result);
return result;
}
2003-01-20 12:10:30 +01:00
void JrdMemoryPool::deletePool(JrdMemoryPool* pool) {
DBB dbb = GET_DBB;
dbb::pool_vec_type::iterator itr =
std::find(dbb->dbb_pools.begin(), dbb->dbb_pools.end(), pool);
if (itr != dbb->dbb_pools.end()) dbb->dbb_pools.erase(itr);
2003-01-20 12:10:30 +01:00
pool->lls_cache.~BlockCache<lls>();
MemoryPool::deletePool(pool);
}
2001-12-24 03:51:06 +01:00
TEXT* ALL_cstring(TEXT* in_string)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* A L L _ c s t r i n g
*
**************************************
*
* Functional description
* Copy a stack local string to an allocated
* string so it doesn't disappear before we
* return to the user or where ever.
*
**************************************/
TDBB tdbb;
2001-12-24 03:51:06 +01:00
JrdMemoryPool *pool;
2002-12-10 12:53:53 +01:00
TEXT *p;
size_t length;
2001-05-23 15:26:42 +02:00
tdbb = GET_THREAD_DATA;
if (!(pool = tdbb->tdbb_default)) {
if (tdbb->tdbb_transaction)
pool = tdbb->tdbb_transaction->tra_pool;
else if (tdbb->tdbb_request)
pool = tdbb->tdbb_request->req_pool;
/* theoretically this shouldn't happen, but just in case */
if (!pool)
return NULL;
}
length = strlen(in_string);
p = FB_NEW(*pool) TEXT[length+1];
strcpy(p,in_string);
return p;
2001-05-23 15:26:42 +02:00
}
void ALL_fini(void)
{
/**************************************
*
* A L L _ f i n i
*
**************************************
*
* Functional description
* Get rid of everything.
* NOTE: This routine does not lock any mutexes on
* its own behalf. It is assumed that mutexes will
* have been locked before entry.
* Call gds__free explicitly instead of ALL_free
* because it references the dbb block which gets
* released at the top of this routine.
*
**************************************/
DBB dbb;
dbb = GET_DBB;
2001-12-24 03:51:06 +01:00
/* Don't know if we even need to do this, so it is commented out */
//delete dbb;
2001-05-23 15:26:42 +02:00
}
void ALL_init(void)
{
/**************************************
*
* A L L _ i n i t
*
**************************************
*
* Functional description
* Initialize the pool system.
* NOTE: This routine does not lock any mutexes on
* its own behalf. It is assumed that mutexes will
* have been locked before entry.
*
**************************************/
TDBB tdbb;
DBB dbb;
2001-12-24 03:51:06 +01:00
JrdMemoryPool* pool;
2001-05-23 15:26:42 +02:00
tdbb = GET_THREAD_DATA;
dbb = tdbb->tdbb_database;
2001-12-24 03:51:06 +01:00
pool = tdbb->tdbb_default = dbb->dbb_permanent;
2003-01-20 12:10:30 +01:00
// dbb->dbb_permanent->setExtendSize(PERM_EXTEND_SIZE);
2001-12-24 03:51:06 +01:00
dbb->dbb_pools[0] = pool;
dbb->dbb_bufferpool = JrdMemoryPool::createPool();
2003-01-20 12:10:30 +01:00
// FB_NEW(*pool) JrdMemoryPool(CACH_EXTEND_SIZE);
2001-05-23 15:26:42 +02:00
}
void JrdMemoryPool::ALL_push(BLK object, LLS * stack)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* A L L _ p u s h
*
**************************************
*
* Functional description
* Push an object on an LLS stack.
*
**************************************/
TDBB tdbb;
LLS node;
2001-12-24 03:51:06 +01:00
JrdMemoryPool* pool;
2001-05-23 15:26:42 +02:00
tdbb = GET_THREAD_DATA;
pool = tdbb->tdbb_default;
2001-12-24 03:51:06 +01:00
node = pool->lls_cache.newBlock();
2001-05-23 15:26:42 +02:00
node->lls_object = object;
node->lls_next = *stack;
*stack = node;
}
BLK JrdMemoryPool::ALL_pop(LLS *stack)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* A L L _ p o p
*
**************************************
*
* Functional description
* Pop an object off a linked list stack. Save the node for
* further use.
*
**************************************/
LLS node;
2001-12-24 03:51:06 +01:00
JrdMemoryPool* pool;
2001-05-23 15:26:42 +02:00
BLK object;
node = *stack;
*stack = node->lls_next;
object = node->lls_object;
2001-12-24 03:51:06 +01:00
pool = (JrdMemoryPool*)MemoryPool::blk_pool(node);
pool->lls_cache.returnBlock(node);
2001-05-23 15:26:42 +02:00
return object;
}
#ifdef SUPERSERVER
2001-12-24 03:51:06 +01:00
void ALL_print_memory_pool_info(IB_FILE* fptr, DBB databases)
2001-05-23 15:26:42 +02:00
{
#ifdef NOT_USED_OR_REPLACED
2001-05-23 15:26:42 +02:00
/***********************************************************
*
* A L L _ p r i n t _ m e m o r y _ p o o l _ i n f o
*
***********************************************************
*
* Functional description
* Print the different block types allocated in the pool.
* Walk the dbb's to print out pool info of every database
*
**************************************/
DBB dbb;
STR string;
VEC vector;
HNK hnk;
ATT att;
int i, j, k, col;
ib_fprintf(fptr, "\n\tALL_xx block types\n");
ib_fprintf(fptr, "\t------------------");
2001-12-24 03:51:06 +01:00
for (i = 0, col = 0; i < types->type_MAX; i++) {
if (types->all_block_type_count[i]) {
2001-05-23 15:26:42 +02:00
if (col % 5 == 0)
ib_fprintf(fptr, "\n\t");
2001-12-24 03:51:06 +01:00
ib_fprintf(fptr, "%s = %d ", types->ALL_types[i],
types->all_block_type_count[i]);
2001-05-23 15:26:42 +02:00
++col;
}
2001-12-24 03:51:06 +01:00
}
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, "\n");
2001-12-24 03:51:06 +01:00
// TMN: Note. Evil code.
2001-05-23 15:26:42 +02:00
for (i = 0, dbb = databases; dbb; dbb = dbb->dbb_next, ++i);
ib_fprintf(fptr, "\tNo of dbbs = %d\n", i);
2001-12-24 03:51:06 +01:00
for (k = 1, dbb = databases; dbb; dbb = dbb->dbb_next, ++k)
{
2001-05-23 15:26:42 +02:00
string = dbb->dbb_filename;
ib_fprintf(fptr, "\n\t dbb%d -> %s\n", k, string->str_data);
vector = (VEC) dbb->dbb_pools;
2001-12-24 03:51:06 +01:00
j = 0;
for (pool_vec_type::iterator itr = dbb->dbb_pools.begin();
itr != dbb->dbb_pools.end(); ++itr)
{
PLB myPool = *itr;
if (myPool) {
2001-05-23 15:26:42 +02:00
++j;
2001-12-24 03:51:06 +01:00
}
2001-05-23 15:26:42 +02:00
}
ib_fprintf(fptr, "\t %s has %d pools", string->str_data, j);
for (j = 0, att = dbb->dbb_attachments; att; att = att->att_next)
2001-12-24 03:51:06 +01:00
{
2001-05-23 15:26:42 +02:00
j++;
2001-12-24 03:51:06 +01:00
}
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, " and %d attachment(s)", j);
2001-12-24 03:51:06 +01:00
for (i = 0; i < (int) vector->vec_count; i++)
{
PLB myPool = (PLB) vector->vec_object[i];
if (!myPool) {
2001-05-23 15:26:42 +02:00
continue;
2001-12-24 03:51:06 +01:00
}
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, "\n\t Pool %d", myPool->plb_pool_id);
2001-12-24 03:51:06 +01:00
// Count # of hunks
for (j = 0, hnk = myPool->plb_hunks; hnk; hnk = hnk->hnk_next) {
++j;
}
if (j) {
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, " has %d hunks", j);
2001-12-24 03:51:06 +01:00
}
j = 0;
// Count # of "huge" hunks
for (hnk = myPool->plb_huge_hunks; hnk; hnk = hnk->hnk_next)
{
++j;
}
if (j) {
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, " and %d huge_hunks", j);
2001-12-24 03:51:06 +01:00
}
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, " Extend size is %d", myPool->plb_extend_size);
2001-12-24 03:51:06 +01:00
for (j = 0, col = 0; j < types->type_MAX; j++)
{
if (myPool->plb_blk_type_count[j])
{
2001-05-23 15:26:42 +02:00
if (col % 5 == 0)
2001-12-24 03:51:06 +01:00
{
2001-05-23 15:26:42 +02:00
ib_fprintf(fptr, "\n\t ");
2001-12-24 03:51:06 +01:00
}
ib_fprintf(fptr, "%s = %d ", types->ALL_types[j],
2001-05-23 15:26:42 +02:00
myPool->plb_blk_type_count[j]);
++col;
}
}
}
}
2001-12-24 03:51:06 +01:00
#endif // 0
2001-05-23 15:26:42 +02:00
}
2001-12-24 03:51:06 +01:00
#endif // SUPERSERVER
2001-05-23 15:26:42 +02:00