/cpublaster.c (57ad1562f696c79c50fb9201a5683057b1d46e94) (3909 bytes) (mode 100644) (type blob)
/*
* Description: Enforce allocation offset tweaking to avoid CPU problems
* Author: Catalin(ux) M. BOIE
* E-mail: catab at embedromix dot ro
* Web: http://kernel.embedromix.ro/us/
*/
#define __USE_GNU
#define _GNU_SOURCE
#define __USE_XOPEN2K
#define __USE_LARGEFILE64
#define __USE_FILE_OFFSET64
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <dlfcn.h>
#include "cpublaster_config.h"
#define HASH_SIZE 32
extern __typeof (malloc) __libc_malloc;
extern __typeof (realloc) __libc_realloc;
extern __typeof (free) __libc_free;
struct node
{
void *ptr;
unsigned int offset;
struct node *next;
};
struct info
{
struct node *head, *tail;
};
static unsigned int offset;
static unsigned int offset_increment = 64;
static unsigned int offset_max = 4 * 4096;
static unsigned int minim_allocation = 128 * 1024;
static unsigned int verbose;
static struct info hash[HASH_SIZE];
static struct node *get(const void *ptr)
{
struct node *p;
unsigned int h;
if (ptr == NULL)
return NULL;
h = ((unsigned int ) ptr >> 16) % HASH_SIZE;
p = hash[h].head;
while (p != NULL) {
if (p->ptr == ptr)
return p;
p = p->next;
}
return NULL;
}
static void add(void *ptr, unsigned int offset)
{
struct node *p;
unsigned int h;
if (ptr == NULL)
return;
h = ((unsigned int) ptr >> 16) % HASH_SIZE;
/* Try to find a free location */
p = hash[h].head;
while (p != NULL) {
if (p->ptr == 0) {
p->ptr = ptr;
break;
}
p = p->next;
}
if (p == NULL) {
p = (struct node *) __libc_malloc(sizeof(struct node));
if (p == NULL)
return;
p->next = NULL;
p->ptr = ptr;
if (hash[h].tail == NULL) {
hash[h].head = p;
} else {
hash[h].tail->next = p;
}
hash[h].tail = p;
}
p->offset = offset;
}
static int del(const void *ptr)
{
struct node *p;
unsigned int h;
int ret = -1;
if (ptr == NULL)
return -1;
h = ((unsigned int) ptr >> 16) % HASH_SIZE;
p = hash[h].head;
while (p != NULL) {
if (p->ptr == ptr) {
p->ptr = 0;
ret = p->offset;
break;
}
p = p->next;
}
return ret;
}
static void init(void)
{
static unsigned char inited = 0;
char *x;
if (inited == 1)
return;
x = getenv("CPUBLASTER_VERBOSE");
if (x)
verbose = strtol(x, NULL, 10);
x = getenv("CPUBLASTER_MINIM_ALLOCATION");
if (x)
minim_allocation = strtol(x, NULL, 10);
offset = (rand() % offset_max) / offset_increment * offset_increment;
inited = 1;
}
void *malloc(size_t size)
{
void *ret;
char buf[256];
init();
if (size < minim_allocation)
return __libc_malloc(size);
ret = __libc_malloc(offset + size);
if (ret == NULL)
return NULL;
if (verbose > 0) {
snprintf(buf, sizeof(buf), "malloc(%zu) offset=%u ret=%p\n",
size, offset, ret + offset);
write(2, buf, strlen(buf));
}
ret += offset;
add(ret, offset);
offset += offset_increment;
if (offset > offset_max)
offset = offset_increment;
return ret;
}
void *realloc(void *ptr, size_t size)
{
char buf[256];
struct node *p;
void *new;
p = get(ptr);
if (p == NULL)
return __libc_realloc(ptr, size);
new = __libc_realloc(ptr - p->offset, p->offset + size);
if (new == NULL)
return NULL;
new += p->offset;
del(ptr);
add(new, p->offset);
if (verbose > 0) {
snprintf(buf, sizeof(buf), "realloc(%p, %zu) offset=%u ret=%p\n",
ptr, size, p->offset, new);
write(2, buf, strlen(buf));
}
return new;
}
void free(void *ptr)
{
char buf[256];
int off;
init();
if (ptr == NULL)
return;
off = del(ptr);
if (off != -1) {
if (verbose > 0) {
snprintf(buf, sizeof(buf), "free(%p) off=%d new_ptr=%p\n",
ptr, off, ptr - off);
write(2, buf, strlen(buf));
}
ptr -= off;
}
__libc_free(ptr);
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
55 |
8b15ae5c6fee7bb3cbe5064779a02bf7904ab89b |
.gitignore |
100644 |
blob |
30 |
d987fa5df957830331139935d517009e2911b0cf |
INSTALL |
100644 |
blob |
35147 |
94a9ed024d3859793618152ea559a168bbcbb5e2 |
LICENSE |
100644 |
blob |
570 |
07da5860299c56ed8ff10bbe14d4d5e162130760 |
Makefile.in |
100644 |
blob |
4144 |
9b49b87643bb4d9f2e221f31ceaeac0ce5e40dd9 |
README |
100644 |
blob |
71 |
7e288d6d080c2009cc5de6b872c75bd64cf2a24f |
TODO |
100755 |
blob |
23 |
d33bb6c4ecdce1390ce1db3c79ea3b93e22ea755 |
configure |
100644 |
blob |
3909 |
57ad1562f696c79c50fb9201a5683057b1d46e94 |
cpublaster.c |
100644 |
blob |
960 |
bc901b273cb498a15f1534ef6bfcbc49df154ee7 |
cpublaster.spec.in |
100644 |
blob |
35 |
379b2c49de4b5be67ff0f328630c921cbc4c620b |
cpublaster_config.h.in |
100755 |
blob |
13141 |
081d1c2174db44f5834f6bf6eb2cf4e805faa2d2 |
duilder |
100644 |
blob |
292 |
578b1b180de42341a75737b31a6f036a51c00be5 |
duilder.conf |
100644 |
blob |
2938 |
8f32fbe195b6f4528e0b0bb04b8bef88ccf7628a |
test1.c |
100755 |
blob |
188 |
4e36772b5daa9c6940bd35b1e0765295dc9fe3e7 |
test1.sh |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/cpublaster
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/cpublaster
Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/cpublaster
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main