PASTEBIN
| #1 paste tool since 2002
create new paste
tools
api
archive
faq
PASTEBIN
create new paste
trending pastes
sign up
login
my alerts
my settings
my profile
Don't like ads?
PRO users
don't see any ads ;-)
Public Pastes
Untitled
1 sec ago
Untitled
4 sec ago
Untitled
5 sec ago
Untitled
7 sec ago
Untitled
7 sec ago
3.7.10-gentoo-r1
10 sec ago
Untitled
12 sec ago
Untitled
14 sec ago
New Paste
diff --git a/a.exe b/a.exe deleted file mode 100644 index fa446ee..0000000 Binary files a/a.exe and /dev/null differ diff --git a/alloc.c b/alloc.c index 846257d..6a01689 100644 --- a/alloc.c +++ b/alloc.c @@ -2,83 +2,16 @@ #define M2 #include <stdlib.h> -#include "structs.h" -#include "hashmap.c" #include "math.h" #include <iostream> -// debug info +const int DATA_LEN = 3; +const int BUFF_SIZE = 1024 * 1024; +const int ALIGN_SIZE = 128; -/* - * Debugging info when AllocOnBuff is called - * Happens when trying to get memory after last node at given buffer. - */ -#define DBGALLOCONBUFF 1 - -/* -* Debugging of execution of function "get_buffer_size". -*/ -#define DBG_BUFFER_SIZE 1 - -/* -* Value [in bytes] of alingment of all returned poitners. -*/ -const size_t ALIGN_SIZE = 16; - - - -/* -* Debug information about reusing memory chunks. -*/ -#define DBG_REUSE_CHUNKS_AT_ALLOC - -//#define EXIT_ON_MALLOC_COUNT 0 -const int MAX_ALLOC_CNT = 3; -extern int new_calls; -extern int del_calls; - -#ifdef CACHE_NODES - //Nodes cache; - const int NODE_CACHE_SIZE = 20 * 1024 * 1024; - int _nodes_cache_used; - Node _nodes_cache[NODE_CACHE_SIZE]; -#endif - -// main buffrs -extern Buffer *_buffers; -extern UnusedBuffer *_unused; - -int total_allocated; -int alloc_count; -int max_alloc; - -int get_buffer_size(int min_size) -{ - int buffAmount = 0; int newSize; - - Buffer *ptr = _buffers; - - while(ptr) - { - buffAmount++; - ptr = ptr->next; - } - - newSize = buffAmount == 0 ? INIT_BUFF_SIZE : pow(2,buffAmount + 1); - - if(newSize <= min_size) - newSize = min_size; - - // 1st added ALIGN_SIZE for first cell, - // second - for memory area of requested data. - newSize += ALIGN_SIZE * 2; - - #ifdef DBG_BUFFER_SIZE - std::cout<<"get_buffer_size: RETURNING SIZE: "<<newSize<<std::endl; - #endif - - return newSize; -} +char *data[DATA_LEN]; +char *ptr[DATA_LEN]; +int dt_cnt; /* * Get the lowest value which is dividable by ALIGN_SIZE and which is no lower than passed value "address". @@ -91,213 +24,29 @@ size_t ceil_addr(size_t address) return address; } -Node* CreateNode(Node *prev, Node *next) -{ - Node *ret; - #ifdef CACHE_NODES - if(_nodes_cache_used < NODE_CACHE_SIZE) - ret = &(_nodes_cache[_nodes_cache_used++]); - else - #endif - ret = (Node *)malloc(sizeof(Node)); - - ret->prev = prev; - ret->next = next; - - ret->used = 1; - - return ret; -} - -Buffer *CreateBuffer(size_t size) +void *alloc(size_t sz) { - Buffer *ret = (Buffer *)malloc(sizeof(Buffer)); - - ret->data = (char *)malloc(size * sizeof(char)); - ret->size = size; - ret->next = NULL; - - Node *actNode = CreateNode(NULL, NULL); - - actNode->buff = ret; - actNode->offset = 0; - actNode->size = 1; - - // - // Set size so that new node would be aligned correctly. - // - // TODO: (proposal, think about it) move node creation code to one function (now there are two places where it's done) - size_t act_addr = (size_t)ret->data; - // - // Calculate the nearest alingned address. - // - size_t req_addr = ceil_addr(act_addr); - size_t new_size = req_addr - act_addr; - // - // I don't want two cells would point to same location, so don't let so that size would be 0. - // - actNode->size = (new_size == 0) ? ALIGN_SIZE : new_size; - - - ret->cells = actNode; - - // - // Set buffer's last node. - // - ret->last = actNode; - - return ret; + return ptr[dt_cnt++]; } -// adds node after a cell, connects cell to node properly -// set's offset and size of node -Node *AllocNew(Node *cell, size_t sz) +void dealloc(void *p) { - Node *ret = CreateNode(cell, cell->next); - - ret->offset = cell->offset + cell->size; - ret->size = sz; - - // node before should point ot ret - cell->next = ret; - - return ret; } -int guaranted_size_onf_buff(Buffer *buff, size_t sz) -{ - int free = (int)((int)buff->size - (int)(buff->last->offset + buff->last->size) ); - if(free <= 0) - return 0; - - return ((sz) <= (size_t) free) ? 1 : 0; -} -// always try to allocate at the end of the buffer -void* AllocOnBuff(Buffer* buff, size_t sz) +void inicialise_allocator() { - Node *new_node; - void *ret; - - size_t req_size = ceil_addr(sz);// enforce so that new address (after current) would be aligned correctly + for(int i=0; i<DATA_LEN; i++) + { + data[i] = malloc(sizeof(char) * BUFF_SIZE); - // - // If there's no space left on buffer. - // - if(!guaranted_size_onf_buff(buff, req_size)) - return NULL; - - new_node = AllocNew(buff->last, req_size); - new_node->buff = buff; - - buff->last = new_node; - - - // add new node to hash map - add_cell(new_node); - - ret = (void *)(buff->data + new_node->offset); - - #ifdef DBGALLOCONBUFF - std::cout<<"sz: "<<sz<<" rs: "<<req_size<<" *p: "<<ret<<" buff->data: "<< (void *)(buff->data)<<" buff->size: "<<buff->size<<" first cell size: "<<buff->cells->size<<std::endl; - #endif - return ret; -} - -Node *try_alloc_unused_buffer(size_t sz) -{ - /* try to reuse existing buffer */ - UnusedBuffer *bf = _unused, *prev = NULL; - while(bf) - { - if(bf->cell) // enforces we don't reuse first block of reused - { - if(bf->cell->size >= sz)// found free cell to reallocate - { - prev->next = bf->next; - bf->cell->used = 1; + size_t new_addr = ceil_addr(data[i]); + + if(new_addr = data[i]) + new_addr += ALIGN_SIZE; + + ptr[i] = new_addr; + } - return bf->cell; - } - } - prev = bf; - bf = bf->next; - } - - return NULL; -} - -void *alloc(size_t sz) -{ - void *ret = NULL; - Buffer *buff = _buffers; - - #ifdef REUSE_CHUNKS - Node *n = NULL; - - // - // Try to reuse some memory chunk. - // - n = try_alloc_unused_buffer(sz); - if(n) - { - ret = (void *)(n->buff->data + n->offset); - - #ifdef DBG_REUSE_CHUNKS_AT_ALLOC - std::cout<<"reusing chunk, returned pointer: "<<ret<<std::endl; - #endif - - return ret; - } - - #endif - - // - // Try to allocate on existing buffer. - // - do{ - ret = AllocOnBuff(buff, sz); - } while( (ret == NULL) && (buff = buff->next) ); - - if(ret) - return ret; - - // - // Breate new buffer. - // - if(!ret) - { - // find last buffer - buff = _buffers; - while(buff->next) - buff = buff->next; - - buff->next = CreateBuffer( get_buffer_size(sz) ); - return AllocOnBuff(buff->next, sz); - } - - return NULL; -} - -void inicialise_allocator() -{ - #ifdef CACHE_NODES - _nodes_cache_used = 0; - #endif - total_allocated = 0; - alloc_count = 0; - max_alloc = 0; - new_calls = 0; - del_calls = 0; - - #ifdef REUSE_CHUNKS - // add empty node - head of the unused nodes list - _unused = (UnusedBuffer *)malloc(sizeof(UnusedBuffer)); - _unused->next = NULL; - _unused->cell = NULL; - #endif - - initialise_hashmaps(); - - _buffers = CreateBuffer( get_buffer_size(0) ); + data_cnt = 0; } #endif diff --git a/dealloc.c b/dealloc.c deleted file mode 100644 index 8a94065..0000000 --- a/dealloc.c +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef DEALLOC -#define DEALLOC - -#include "structs.h" -#include <iostream> - - -extern Buffer *_buffers; -extern UnusedBuffer *_unused; - -void mark_unused(Node *cell) -{ - #ifdef PRINT_CALL_MAP - std::cout<<"U"; - #endif - // set flag - cell->used = 0; - UnusedBuffer *prev, *act = _unused; - - // add to list of unused nodes - do { - prev = act; - } while((act = act->next)); - - // configure entry about new unused buffer - prev->next = (UnusedBuffer *)malloc(sizeof(UnusedBuffer)); - prev->next->next = NULL; //remove these ugly -> chains.... - prev->next->cell = cell; -} - -void dealloc(void *p) -{ - #ifdef PRINT_DEALLOC_CNT_EACH_TIME - std::cout<<"DEALLOC CALLED, *p: "<<p<<std::endl; - #endif - if((size_t)p == 0x10) - std::cout<<"ERR"<<std::endl; -// Buffer *buffer = _buffers; - Node *cell = NULL; - - cell = find_cell((size_t)p); - - #ifdef REUSE_CHUNKS - mark_unused(cell); - #endif -/* for(;buffer;buffer = buffer->next) - { - for(cell = buffer->cells; cell; cell = cell->next) - { - if(cell->offset + buffer->data == p) - { - mark_unused(cell); - break; - } - } - }*/ -} -#endif - diff --git a/diff b/diff deleted file mode 100644 index e69de29..0000000 diff --git a/ff.txt b/ff.txt deleted file mode 100644 index 03f3c0a..0000000 --- a/ff.txt +++ /dev/null @@ -1,5 +0,0 @@ -Tomek -Romek -Slawek -Marek -A \ No newline at end of file diff --git a/hashmap.c b/hashmap.c deleted file mode 100644 index 775d66f..0000000 --- a/hashmap.c +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef HASHMAP_C -#define HASHMAP_C - -#include "structs.h" - -extern Buffer *_buffers; -const int D_FACTOR = 1000; -const int ENTRY_LEN = (INIT_BUFF_SIZE / D_FACTOR) > 0 ? (INIT_BUFF_SIZE / D_FACTOR) : 1; -typedef struct Entry Entry; -struct Entry { - size_t offset; - size_t addr; - Node *cell; - Entry *next; -}; - -Entry *_entries[ENTRY_LEN]; - -Entry *CreateEntry(Node *cell) -{ - Entry *ret = (Entry *) malloc(sizeof(Entry)); - - ret->cell = cell; - ret->next = NULL; - - if(cell) - { - ret->offset = cell->offset; - ret->addr = cell->offset + (size_t)cell->buff->data; - } - else - { - ret->offset = 0; - ret->addr = 0; - } - - return ret; -} - -int calculate_hash(size_t value) -{ - return value % ENTRY_LEN; -} - -void add_cell(Node *cell) -{ - #ifdef PRINT_CALL_MAP - std::cout<<"P"; - #endif - int key = calculate_hash((size_t)(cell->offset + cell->buff->data)); - Entry *en = _entries[key]; - - while(en->next) - en = en->next; - - en->next = CreateEntry(cell); -} - -Node *find_cell(size_t address) -{ - #ifdef PRINT_CALL_MAP - std::cout<<"F"; - #endif - int key = calculate_hash(address); - Entry *en = _entries[key]; - - while( en && (en->addr != address)) - en = en->next; - - if(en) - return en->cell; - return NULL; -} - -void initialise_hashmaps() -{ - for(int i=0;i<ENTRY_LEN;i++) - { - _entries[i] = CreateEntry(NULL); - } -} -#endif diff --git a/main.cpp b/main.cpp index f67fb0d..9a6d5e8 100644 --- a/main.cpp +++ b/main.cpp @@ -7,16 +7,10 @@ #include <iostream> #include <fstream> -#include "my_allocator.h" - -//#define DBGNEW - - -extern int new_calls; -extern int del_calls; -extern Buffer *_buffers; -const int EXTRA_CONTENT = 0; +#include "alloc.c" +int new_calls; +int del_calls; #define M diff --git a/my_allocator.h b/my_allocator.h deleted file mode 100644 index f39f481..0000000 --- a/my_allocator.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef MY_ALLOCATOR_H -#define MY_ALLOCATOR_H - -#include "alloc.c" -#include "dealloc.c" - -#endif diff --git a/simple_case.cpp b/simple_case.cpp deleted file mode 100644 index 2cc2f4a..0000000 --- a/simple_case.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "stdlib.h" -#include <iostream> -#include "my_allocator.h" - - inline void* operator new(size_t sz) - { - void *ret = alloc(sz); - } - - inline void operator delete(void *p) - { - dealloc(p); - } - - void * operator new [] (size_t size){ - void *ret = alloc(size); - return ret; - } - void operator delete [] (void * p){ - return dealloc(p); - } - -int main() -{ - inicialise_allocator(); - std::string *ptr = new std::string[10]; - delete [] ptr; - - return 0; -} diff --git a/structs.h b/structs.h deleted file mode 100644 index 75b7cdf..0000000 --- a/structs.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef STRUCTS_H -#define STRUCTS_H - -typedef struct Buffer; - -typedef struct Node Node; -struct Node{ - Node *next; - Node *prev; - size_t offset; - size_t size; - /* - * Containing Buffer. - */ - Buffer *buff; - int used; -}; - -// TODO: Buffer should have pointer to it's last entry -typedef struct Buffer Buffer; -struct Buffer{ - size_t size; - char *data; - Node *cells; - - /* - * Last node of the buffer. That node has has allocated memory. - */ - Node *last; - - Buffer *next; -}; - -typedef struct UnusedBuffer UnusedBuffer; -struct UnusedBuffer { - Node *cell; - UnusedBuffer *next; -}; - - -#define INIT_BUFF_SIZE 6 -//#define PRINT_CALL_MAP -// -#define PRINT_DEALLOC_CNT_EACH_TIME 1 - -Buffer *_buffers; -UnusedBuffer *_unused; - -int new_calls; -int del_calls; - -#define REUSE_CHUNKS 1 -//#define CACHE_NODES 1 -#endif
Optional Paste Settings
Syntax Highlighting:
None
Bash
C
C#
C++
CSS
HTML
HTML 5
Java
JavaScript
Lua
None
Objective C
Perl
PHP
Python
Rails
-------------
4CS
6502 ACME Cross Assembler
6502 Kick Assembler
6502 TASM/64TASS
ABAP
ActionScript
ActionScript 3
Ada
ALGOL 68
Apache Log
AppleScript
APT Sources
ARM
ASM (NASM)
ASP
Asymptote
autoconf
Autohotkey
AutoIt
Avisynth
Awk
BASCOM AVR
Bash
Basic4GL
BibTeX
Blitz Basic
BNF
BOO
BrainFuck
C
C for Macs
C Intermediate Language
C#
C++
C++ (with QT extensions)
C: Loadrunner
CAD DCL
CAD Lisp
CFDG
ChaiScript
Clojure
Clone C
Clone C++
CMake
COBOL
CoffeeScript
ColdFusion
CSS
Cuesheet
D
DCL
DCPU-16
DCS
Delphi
Delphi Prism (Oxygene)
Diff
DIV
DOS
DOT
E
ECMAScript
Eiffel
Email
EPC
Erlang
F#
Falcon
FO Language
Formula One
Fortran
FreeBasic
FreeSWITCH
GAMBAS
Game Maker
GDB
Genero
Genie
GetText
Go
Groovy
GwBasic
Haskell
Haxe
HicEst
HQ9 Plus
HTML
HTML 5
Icon
IDL
INI file
Inno Script
INTERCAL
IO
J
Java
Java 5
JavaScript
jQuery
KiXtart
Latex
LDIF
Liberty BASIC
Linden Scripting
Lisp
LLVM
Loco Basic
Logtalk
LOL Code
Lotus Formulas
Lotus Script
LScript
Lua
M68000 Assembler
MagikSF
Make
MapBasic
MatLab
mIRC
MIX Assembler
Modula 2
Modula 3
Motorola 68000 HiSoft Dev
MPASM
MXML
MySQL
Nagios
newLISP
None
NullSoft Installer
Oberon 2
Objeck Programming Langua
Objective C
OCalm Brief
OCaml
Octave
OpenBSD PACKET FILTER
OpenGL Shading
Openoffice BASIC
Oracle 11
Oracle 8
Oz
ParaSail
PARI/GP
Pascal
PAWN
PCRE
Per
Perl
Perl 6
PHP
PHP Brief
Pic 16
Pike
Pixel Bender
PL/SQL
PostgreSQL
POV-Ray
Power Shell
PowerBuilder
ProFTPd
Progress
Prolog
Properties
ProvideX
PureBasic
PyCon
Python
Python for S60
q/kdb+
QBasic
R
Rails
REBOL
REG
Rexx
Robots
RPM Spec
Ruby
Ruby Gnuplot
SAS
Scala
Scheme
Scilab
SdlBasic
Smalltalk
Smarty
SPARK
SPARQL
SQL
StoneScript
SystemVerilog
T-SQL
TCL
Tera Term
thinBasic
TypoScript
Unicon
UnrealScript
UPC
Urbi
Vala
VB.NET
Vedit
VeriLog
VHDL
VIM
Visual Pro Log
VisualBasic
VisualFoxPro
WhiteSpace
WHOIS
Winbatch
XBasic
XML
Xorg Config
XPP
YAML
Z80 Assembler
ZXBasic
Paste Expiration:
Never
10 Minutes
1 Hour
1 Day
1 Week
2 Weeks
1 Month
Paste Exposure:
Public
Unlisted
Private (members only)
Paste Name / Title:
Hello
Guest
Sign Up
or
Login
You are currently not logged in, this means you can not edit or delete anything you paste.
Sign Up
or
Login