object grow_buffer(object old, long new_capacity) { long old_capacity = BUFFER_CAPACITY(old); long old_length = BUFFER_LENGTH(old); if (old_capacity >= new_capacity) return old; gc_tmp1 = old; gc_tmp2 = make_buffer(new_capacity); memcpy(BUFFER_DATA(gc_tmp2),BUFFER_DATA(gc_tmp1),old_length); BUFFER_LENGTH(gc_tmp2) = BUFFER_LENGTH(old); return gc_tmp2; }
void ccnet_packet_finish (CcnetPacketIO *io) { ccnet_header *header; header = (ccnet_header *) BUFFER_DATA(io->buffer); header->length = htons (BUFFER_LENGTH(io->buffer) - CCNET_PACKET_LENGTH_HEADER); }
///////////////////////////////////////////////////////////////////////////// // HRESULT CMallocSpy::DumpLeaks // ///////////////////////////////////////////////////////////////////////////// HRESULT CMallocSpy::DumpLeaks() { ULONG cTotalLeaks = 0; SIZE_T cTotalBytes = 0; DWORD dwSelection = IDOK; //Display Leaks to the Output Window while(!CAllocList.IsEmpty()) { //Obtain the pointer to the leaked memory void* pRequest = CAllocList.RemoveHead(); ASSERT(pRequest); void* pActual = HEADER_OFFSET(pRequest); ASSERT(pActual); //Make sure that the head/tail signitures are intact if(HEAD_SIGNITURE(pActual) != HEADSIGN) InternalTraceFmt(L"TRACE - IMallocSpy HeadSigniture Corrupted! - 0x%p, ID=%08lu, %Iu byte(s)\n", pRequest, BUFFER_ID(pActual), BUFFER_LENGTH(pActual)); if(TAIL_SIGNITURE(pActual) != TAILSIGN) InternalTraceFmt(L"TRACE - IMallocSpy TailSigniture Corrupted! - 0x%p, ID=%08lu, %Iu byte(s)\n", pRequest, BUFFER_ID(pActual), BUFFER_LENGTH(pActual)); SIZE_T ulSize = BUFFER_LENGTH(pActual); ULONG ulID = BUFFER_ID(pActual); WCHAR* pwszFileName= BUFFER_FILENAME(pActual); ULONG ulLine = BUFFER_LINENUMBER(pActual); //Display a message box for all leaks (until the user is tired of it...) if(dwSelection == IDOK) { dwSelection = wMessageBox(GetFocus(), MB_TASKMODAL | MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON1, L"IMallocSpy Leak", L"IMallocSpy Leak:\n" L"0x%p, ID=%08lu, %Iu byte(s), File: %s, Line %d\n\n", pRequest, ulID, ulSize, pwszFileName, ulLine); } //Include FileName and Line Number of the leak... InternalTraceFmt(L"-- IMallocSpy Leak! - 0x%p,\tID=%08lu,\t%Iu byte(s),\tFile: %s,\tLine %d" wWndEOL, pRequest, ulID, ulSize, pwszFileName, ulLine); cTotalLeaks++; cTotalBytes += ulSize; //Free the Leak //You really cant free the leak since the app could be potentially still //using it. Or the DLL may still be in use or have attached threads... //SAFE_FREE(pActual); } if(cTotalLeaks) InternalTraceFmt(L"-- IMallocSpy Total Leaks: %lu = %Iu byte(s)" wWndEOL, cTotalLeaks, cTotalBytes); return S_OK; }
void ccnet_packet_prepare (CcnetPacketIO *io, int type, int id) { ccnet_header header; assert (io->buffer && BUFFER_LENGTH(io->buffer) == 0); header.version = 1; header.type = type; header.length = 0; header.id = htonl (id); buffer_add (io->buffer, &header, sizeof (header)); }
/* return 0 on EOF, -1 on error, 1 otherwise */ int ccnet_packet_io_read (CcnetPacketIO *io) { int n; ccnet_packet *packet; int len; again: if ( (n = buffer_read(io->in_buf, io->fd, 1024)) < 0) { if (errno == EINTR) goto again; g_warning ("read from connfd error: %s.\n", strerror(errno)); return -1; } if (n == 0) { if (io->func) io->func (NULL, io->user_data); return 0; } while (BUFFER_LENGTH(io->in_buf) >= CCNET_PACKET_LENGTH_HEADER) { packet = (ccnet_packet *) BUFFER_DATA(io->in_buf); len = ntohs (packet->header.length); if (BUFFER_LENGTH (io->in_buf) - CCNET_PACKET_LENGTH_HEADER < len) break; packet->header.length = len; packet->header.id = ntohl (packet->header.id); io->func (packet, io->user_data); buffer_drain (io->in_buf, len + CCNET_PACKET_LENGTH_HEADER); } return 1; }
object make_procedure(object opbuffer, object name, long argc) { long op_size = BUFFER_LENGTH(opbuffer); object result; gc_tmp2 = name; gc_tmp3 = opbuffer; result = make_heap_object(PROCEDURE_TYPE, sizeof(struct proc_heap_structure)); PROC_OPS(result) = (long *)allocate_code_space(op_size); memcpy(PROC_OPS(result),BUFFER_DATA(gc_tmp3),op_size); PROC_MODULE(result) = gc_tmp2; PROC_ARGC(result) = argc; return result; }
///////////////////////////////////////////////////////////////////////////// // void* CMallocSpy::PostRealloc // ///////////////////////////////////////////////////////////////////////////// void* CMallocSpy::PostRealloc(void* pActual, BOOL fSpyed) { //E_OUTOFMEMORY condition if(!pActual) return NULL; //If this buffer was alloced under IMallocSpy if(fSpyed) { //pActual is the pointer to header //Add the new pointer to the list AddToList(USERS_OFFSET(pActual)); //HeadSigniture should still be intact if(HEAD_SIGNITURE(pActual) != HEADSIGN) InternalTraceFmt(L"TRACE - IMallocSpy HeadSigniture Corrupted! - 0x%p, ID=%08lu, %Iu byte(s)\n", USERS_OFFSET(pActual), BUFFER_ID(pActual), BUFFER_LENGTH(pActual)); //ID should still be intact //Place the new Size in the HEADER BUFFER_LENGTH(pActual) = m_cbRequest; //Place the new FileName in the HEADER BUFFER_FILENAME(pActual) = NULL; //Place the new Line Number in the HEADER BUFFER_LINENUMBER(pActual) = 0; //Need to place the tail signiture again, //since it will be over written by the realloc TAIL_SIGNITURE(pActual) = TAILSIGN; //Show ReAllocations if(GetErrorPosting(EP_IMALLOC_ALLOCS)) InternalTraceFmt(L"TRACE - IMallocSpy Realloc - 0x%p,\tID=%08lu,\t%Iu byte(s)\n", USERS_OFFSET(pActual), BUFFER_ID(pActual), m_cbRequest); //Return the actual "user" buffer return USERS_OFFSET(pActual); } //else return pActual; }
///////////////////////////////////////////////////////////////////////////// // void* CMallocSpy::PostAlloc // ///////////////////////////////////////////////////////////////////////////// void* CMallocSpy::PostAlloc(void* pActual) { //E_OUTOFMEMORY condition if(!pActual) return NULL; //pActual is the pointer to the head of the buffer, including the header //Add the users pointer to the list AddToList(USERS_OFFSET(pActual)); //Place the HeadSigniture in the HEADER HEAD_SIGNITURE(pActual) = HEADSIGN; //Place the Size in the HEADER BUFFER_LENGTH(pActual) = m_cbRequest; //Place the ID in the HEADER ULONG ulID = ++m_cAllocations; BUFFER_ID(pActual) = ulID; //Place the FILENAME in the HEADER BUFFER_FILENAME(pActual) = NULL; //Place the LINE NUMBER in the HEADER BUFFER_LINENUMBER(pActual) = 0; //Set the UsersBuffer to a known char memset(USERS_OFFSET(pActual), ALLOCSIGN, m_cbRequest); //Place the TailSigniture in the HEADER TAIL_SIGNITURE(pActual) = TAILSIGN; //Show Allocation if(GetErrorPosting(EP_IMALLOC_ALLOCS)) InternalTraceFmt(L"TRACE - IMallocSpy Alloc - 0x%p,\tID=%08lu,\t%Iu byte(s)\n", USERS_OFFSET(pActual), ulID, m_cbRequest); //Break at indicated Allocation if(g_dwBreakID == ulID) BREAKINTO(); // Return the actual users buffer return USERS_OFFSET(pActual); }
///////////////////////////////////////////////////////////////////////////// // void* CMallocSpy::PreFree // ///////////////////////////////////////////////////////////////////////////// void* CMallocSpy::PreFree(void* pRequest, BOOL fSpyed) { //pRequest is the users pointer to thier buffer, not the header //E_OUTOFMEMORY condition if(!pRequest) return NULL; //If this memory was alloced under IMallocSpy, need to remove it if(fSpyed) { //Remove this pointer from the list RemoveFromList(pRequest); void* pActual = HEADER_OFFSET(pRequest); ULONG ulID = BUFFER_ID(pActual); //Make sure that the head/tail signitures are intact if(HEAD_SIGNITURE(pActual) != HEADSIGN) InternalTraceFmt(L"TRACE - IMallocSpy HeadSigniture Corrupted! - 0x%p, ID=%08lu, %Iu byte(s)\n", pRequest, ulID, BUFFER_LENGTH(pActual)); if(TAIL_SIGNITURE(pActual) != TAILSIGN) InternalTraceFmt(L"TRACE - IMallocSpy TailSigniture Corrupted! - 0x%p, ID=%08lu, %Iu byte(s)\n", pRequest, ulID, BUFFER_LENGTH(pActual)); //Break at indicated Allocation if(g_dwBreakID == ulID) BREAKINTO(); //Set the UsersBuffer to a known char memset(pRequest, FREESIGN, BUFFER_LENGTH(pActual)); //Need to return the actual header pointer to //free the entire buffer including the heading return pActual; } //else return pRequest; }
static SQLRETURN MNDBSpecialColumns(ODBCStmt *stmt, SQLUSMALLINT IdentifierType, SQLCHAR *CatalogName, SQLSMALLINT NameLength1, SQLCHAR *SchemaName, SQLSMALLINT NameLength2, SQLCHAR *TableName, SQLSMALLINT NameLength3, SQLUSMALLINT Scope, SQLUSMALLINT Nullable) { RETCODE rc; /* buffer for the constructed query to do meta data retrieval */ char *query = NULL; char *query_end = NULL; char *cat = NULL, *sch = NULL, *tab = NULL; fixODBCstring(CatalogName, NameLength1, SQLSMALLINT, addStmtError, stmt, return SQL_ERROR); fixODBCstring(SchemaName, NameLength2, SQLSMALLINT, addStmtError, stmt, return SQL_ERROR); fixODBCstring(TableName, NameLength3, SQLSMALLINT, addStmtError, stmt, return SQL_ERROR); #ifdef ODBCDEBUG ODBCLOG("\"%.*s\" \"%.*s\" \"%.*s\" %s %s\n", (int) NameLength1, (char *) CatalogName, (int) NameLength2, (char *) SchemaName, (int) NameLength3, (char *) TableName, translateScope(Scope), translateNullable(Nullable)); #endif /* check for valid IdentifierType argument */ switch (IdentifierType) { case SQL_BEST_ROWID: case SQL_ROWVER: break; default: /* Column type out of range */ addStmtError(stmt, "HY097", NULL, 0); return SQL_ERROR; } /* check for valid Scope argument */ switch (Scope) { case SQL_SCOPE_CURROW: case SQL_SCOPE_TRANSACTION: case SQL_SCOPE_SESSION: break; default: /* Scope type out of range */ addStmtError(stmt, "HY098", NULL, 0); return SQL_ERROR; } /* check for valid Nullable argument */ switch (Nullable) { case SQL_NO_NULLS: case SQL_NULLABLE: break; default: /* Nullable type out of range */ addStmtError(stmt, "HY099", NULL, 0); return SQL_ERROR; } /* check if a valid (non null, not empty) table name is supplied */ if (TableName == NULL) { /* Invalid use of null pointer */ addStmtError(stmt, "HY009", NULL, 0); return SQL_ERROR; } if (NameLength3 == 0) { /* Invalid string or buffer length */ addStmtError(stmt, "HY090", NULL, 0); return SQL_ERROR; } /* SQLSpecialColumns returns a table with the following columns: SMALLINT scope VARCHAR column_name NOT NULL SMALLINT data_type NOT NULL VARCHAR type_name NOT NULL INTEGER column_size INTEGER buffer_length SMALLINT decimal_digits SMALLINT pseudo_column */ if (IdentifierType == SQL_BEST_ROWID) { /* Select from the key table the (smallest) primary/unique key */ if (stmt->Dbc->sql_attr_metadata_id == SQL_FALSE) { if (NameLength1 > 0) { cat = ODBCParseOA("e", "value", (const char *) CatalogName, (size_t) NameLength1); if (cat == NULL) goto nomem; } if (NameLength2 > 0) { sch = ODBCParseOA("s", "name", (const char *) SchemaName, (size_t) NameLength2); if (sch == NULL) goto nomem; } if (NameLength3 > 0) { tab = ODBCParseOA("t", "name", (const char *) TableName, (size_t) NameLength3); if (tab == NULL) goto nomem; } } else { if (NameLength1 > 0) { cat = ODBCParseID("e", "value", (const char *) CatalogName, (size_t) NameLength1); if (cat == NULL) goto nomem; } if (NameLength2 > 0) { sch = ODBCParseID("s", "name", (const char *) SchemaName, (size_t) NameLength2); if (sch == NULL) goto nomem; } if (NameLength3 > 0) { tab = ODBCParseID("t", "name", (const char *) TableName, (size_t) NameLength3); if (tab == NULL) goto nomem; } } /* first create a string buffer (1000 extra bytes is plenty */ query = (char *) malloc(5000 + NameLength1 + NameLength2 + NameLength3); if (query == NULL) goto nomem; query_end = query; /* Note: SCOPE is SQL_SCOPE_TRANSACTION */ /* Note: PSEUDO_COLUMN is SQL_PC_NOT_PSEUDO */ sprintf(query_end, "with sc as (" "select t.id as table_id, k.type as type, " "cast(%d as smallint) as scope, " "c.name as column_name, " DATA_TYPE(c) ", " TYPE_NAME(c) ", " COLUMN_SIZE(c) ", " BUFFER_LENGTH(c) ", " DECIMAL_DIGITS(c) ", " "cast(%d as smallint) as pseudo_column " "from sys.schemas s, " "sys.tables t, " "sys.columns c, " "sys.keys k, " "sys.objects kc, " "sys.env() e " "where s.id = t.schema_id and " "t.id = c.table_id and " "t.id = k.table_id and " "c.name = kc.name and " "kc.id = k.id and " "k.type = 0 and " "e.name = 'gdk_dbname'", /* scope: */ SQL_SCOPE_TRANSACTION, #ifdef DATA_TYPE_ARGS DATA_TYPE_ARGS, #endif #ifdef TYPE_NAME_ARGS TYPE_NAME_ARGS, #endif #ifdef COLUMN_SIZE_ARGS COLUMN_SIZE_ARGS, #endif #ifdef BUFFER_SIZE_ARGS BUFFER_SIZE_ARGS, #endif #ifdef DECIMAL_DIGITS_ARGS DECIMAL_DIGITS_ARGS, #endif /* pseudo_column: */ SQL_PC_NOT_PSEUDO); assert(strlen(query) < 4300); query_end += strlen(query_end); /* TODO: improve the SQL to get the correct result: - only one set of columns should be returned, also when multiple primary keys are available for this table. - when the table has NO primary key it should return the columns of a unique key (only from ONE unique key which is also the best/smallest key) TODO: optimize SQL: - when no SchemaName is set (see above) also no filtering on SCHEMA NAME and join with table SCHEMAS is needed! */ /* add the selection condition */ if (cat) { /* filtering requested on catalog name */ sprintf(query_end, " and %s", cat); query_end += strlen(query_end); free(cat); } if (sch) { /* filtering requested on schema name */ sprintf(query_end, " and %s", sch); query_end += strlen(query_end); free(sch); } if (tab) { /* filtering requested on table name */ sprintf(query_end, " and %s", tab); query_end += strlen(query_end); free(tab); } /* add an extra selection when SQL_NO_NULLS is requested */ if (Nullable == SQL_NO_NULLS) { strcpy(query_end, " and c.\"null\" = false"); query_end += strlen(query_end); } strcpy(query_end, "), " "tid as (" "select t.id as tid " "from sys._tables t, sys.keys k " "where t.id = k.table_id and k.type = 0" ") " "select sc.scope, sc.column_name, sc.data_type, " "sc.type_name, sc.column_size, " "sc.buffer_length, sc.decimal_digits, " "sc.pseudo_column " "from sc " "where (sc.type = 0 and " "sc.table_id in (select tid from tid)) or " "(sc.type = 1 and " "sc.table_id not in (select tid from tid))"); query_end += strlen(query_end); /* ordering on SCOPE not needed (since it is constant) */ } else { assert(IdentifierType == SQL_ROWVER); /* The backend does not have such info available */ /* create just a query which results in zero rows */ /* Note: pseudo_column is sql_pc_unknown is 0 */ query = strdup("select cast(null as smallint) as scope, " "cast('' as varchar(1)) as column_name, " "cast(1 as smallint) as data_type, " "cast('char' as varchar(4)) as type_name, " "cast(1 as integer) as column_size, " "cast(1 as integer) as buffer_length, " "cast(0 as smallint) as decimal_digits, " "cast(0 as smallint) as pseudo_column " "where 0 = 1"); if (query == NULL) goto nomem; query_end = query + strlen(query); } /* query the MonetDB data dictionary tables */ rc = MNDBExecDirect(stmt, (SQLCHAR *) query, (SQLINTEGER) (query_end - query)); free(query); return rc; nomem: /* note that query must be NULL when we get here */ if (cat) free(cat); if (sch) free(sch); if (tab) free(tab); /* Memory allocation error */ addStmtError(stmt, "HY001", NULL, 0); return SQL_ERROR; }
object make_buffer(long capacity) { long size = sizeof(struct buffer_heap_structure) + capacity; object result = make_heap_object(BUFFER_TYPE,size); BUFFER_LENGTH(result) = 0; return result; }