2001-05-23 15:26:42 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD Backup and Restore Program
|
|
|
|
* MODULE: misc.c
|
|
|
|
* DESCRIPTION: Miscellaneous useful routines
|
|
|
|
*
|
|
|
|
* 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): ______________________________________.
|
2002-10-28 05:57:07 +01:00
|
|
|
*
|
|
|
|
* 2002.10.27 Sean Leyne - Code Cleanup, removed obsolete "UNIXWARE" port
|
|
|
|
*
|
2001-05-23 15:26:42 +02:00
|
|
|
*/
|
|
|
|
|
2001-07-30 01:43:24 +02:00
|
|
|
#include "firebird.h"
|
2004-04-29 00:00:03 +02:00
|
|
|
#include <stdio.h>
|
2001-05-23 15:26:42 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include "../burp/burp.h"
|
|
|
|
#include "../burp/burp_proto.h"
|
|
|
|
#include "../burp/misc_proto.h"
|
|
|
|
|
|
|
|
#ifdef SUPERSERVER
|
2004-06-08 15:41:08 +02:00
|
|
|
#include "../jrd/thd.h"
|
2001-05-23 15:26:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
UCHAR *MISC_alloc_burp(ULONG size)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* M I S C _ a l l o c _ b u r p
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Allocate block of memory. Note that it always zeros out memory.
|
|
|
|
* This could be optimized.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
2004-05-20 02:56:59 +02:00
|
|
|
TGBL tdgbl = BURP_get_thread_data();
|
2001-05-23 15:26:42 +02:00
|
|
|
|
2003-09-25 13:49:12 +02:00
|
|
|
// Add some header space to store a list of blocks allocated for this gbak
|
2001-05-23 15:26:42 +02:00
|
|
|
size += ROUNDUP(sizeof(UCHAR *), ALIGNMENT);
|
|
|
|
|
2001-05-24 16:54:26 +02:00
|
|
|
UCHAR* block = (UCHAR*)gds__alloc(size);
|
|
|
|
|
|
|
|
if (!block)
|
2001-05-23 15:26:42 +02:00
|
|
|
/* NOMEM: message & abort FREE: all items freed at gbak exit */
|
|
|
|
{
|
2003-11-03 02:12:14 +01:00
|
|
|
BURP_error(238, true, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
// msg 238: System memory exhaused
|
2001-05-23 15:26:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(block, 0, size);
|
|
|
|
|
|
|
|
/* FREE: We keep a linked list of all gbak memory allocations, which
|
|
|
|
* are then freed when gbak exits. This is important for
|
|
|
|
* NETWARE in particular.
|
|
|
|
*/
|
|
|
|
*((UCHAR **) block) = tdgbl->head_of_mem_list;
|
|
|
|
tdgbl->head_of_mem_list = block;
|
|
|
|
|
|
|
|
return (block + ROUNDUP(sizeof(UCHAR *), ALIGNMENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MISC_free_burp( void *free)
|
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* M I S C _ f r e e _ b u r p
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Release an unwanted block.
|
|
|
|
*
|
|
|
|
**************************************/
|
2004-05-20 02:56:59 +02:00
|
|
|
TGBL tdgbl = BURP_get_thread_data();
|
2001-05-23 15:26:42 +02:00
|
|
|
|
|
|
|
if (free != NULL) {
|
2003-09-25 13:49:12 +02:00
|
|
|
// Point at the head of the allocated block
|
2003-09-12 11:04:38 +02:00
|
|
|
UCHAR **block =
|
2001-05-23 15:26:42 +02:00
|
|
|
(UCHAR **) ((UCHAR *) free - ROUNDUP(sizeof(UCHAR *), ALIGNMENT));
|
|
|
|
|
2003-09-25 13:49:12 +02:00
|
|
|
// Scan for this block in the list of blocks
|
2003-09-12 11:04:38 +02:00
|
|
|
for (UCHAR **ptr = &tdgbl->head_of_mem_list; *ptr; ptr = (UCHAR **) *ptr)
|
|
|
|
{
|
2001-05-23 15:26:42 +02:00
|
|
|
if (*ptr == (UCHAR *) block) {
|
2003-09-25 13:49:12 +02:00
|
|
|
// Found it - remove it from the list
|
2001-05-23 15:26:42 +02:00
|
|
|
*ptr = *block;
|
|
|
|
|
2003-09-25 13:49:12 +02:00
|
|
|
// and free it
|
2001-05-23 15:26:42 +02:00
|
|
|
gds__free((SLONG *) block);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-25 13:49:12 +02:00
|
|
|
// We should always find the block in the list
|
2003-11-03 02:12:14 +01:00
|
|
|
BURP_error(238, true, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
// msg 238: System memory exhausted
|
2003-09-25 13:49:12 +02:00
|
|
|
// (too lazy to add a better message)
|
2001-05-23 15:26:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-09-12 11:04:38 +02:00
|
|
|
// Since this code appears everywhere, it makes more sense to isolate it
|
|
|
|
// in a function visible to all gbak components.
|
|
|
|
// Given a request, if it's non-zero (compiled), deallocate it but
|
|
|
|
// without caring about a possible error.
|
|
|
|
void MISC_release_request_silent(isc_req_handle& req_handle)
|
|
|
|
{
|
|
|
|
if (req_handle)
|
|
|
|
{
|
|
|
|
ISC_STATUS_ARRAY req_status;
|
|
|
|
isc_release_request(req_status, &req_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MISC_terminate(const TEXT* from, TEXT* to, ULONG length, ULONG max_length)
|
2001-05-23 15:26:42 +02:00
|
|
|
{
|
|
|
|
/**************************************
|
|
|
|
*
|
|
|
|
* M I S C _ t e r m i n a t e
|
|
|
|
*
|
|
|
|
**************************************
|
|
|
|
*
|
|
|
|
* Functional description
|
|
|
|
* Null-terminate a possibly non-
|
|
|
|
* null-terminated string with max
|
|
|
|
* buffer room.
|
|
|
|
*
|
|
|
|
**************************************/
|
|
|
|
|
|
|
|
if (length) {
|
|
|
|
length = MIN(length, max_length - 1);
|
2003-09-25 13:49:12 +02:00
|
|
|
do {
|
2001-05-23 15:26:42 +02:00
|
|
|
*to++ = *from++;
|
2003-09-25 13:49:12 +02:00
|
|
|
} while (--length);
|
2001-05-23 15:26:42 +02:00
|
|
|
*to++ = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (max_length-- && (*to++ = *from++));
|
|
|
|
*--to = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|