Added transmission code to the memory collector.
[aesalon.git] / modules / memory / src / collector / memory.c
blob52823f420e62ea554fb787f45437e0c7af90bbea
1 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <errno.h>
5 #include <dlfcn.h>
6 #include <malloc.h>
8 #include "informer/Informer.h"
10 void AC_EXPORT *calloc(size_t nmemb, size_t size);
11 void AC_EXPORT *malloc(size_t size);
12 void AC_EXPORT free(void *ptr);
13 void AC_EXPORT *realloc(void *ptr, size_t size);
15 ModuleID AC_moduleID;
16 void *(*AC_callocFP)(size_t nmemb, size_t size);
17 void *(*AC_mallocFP)(size_t size);
18 void (*AC_freeFP)(void *ptr);
19 void *(*AC_reallocFP)(void *ptr, size_t size);
21 void AC_EXPORT *calloc(size_t nmemb, size_t size) {
22 static int resolving = 0;
23 static char resolvingData[1024];
24 static size_t usedData = 0;
26 if(resolving == 1) {
27 size_t original = usedData;
28 usedData += nmemb * size;
29 return resolvingData + original;
32 if(AC_callocFP == NULL) {
33 resolving = 1;
34 *(void **)&AC_callocFP = dlsym(RTLD_NEXT, "calloc");
35 resolving = 0;
38 void *data = AC_callocFP(nmemb, size);
40 if(AI_CollectionStatus()) {
41 AI_StartPacket(AC_moduleID);
43 *(uint8_t *)AI_PacketSpace(1) = 1;
44 *(uint64_t *)AI_PacketSpace(8) = AI_Timestamp();
45 *(uint64_t *)AI_PacketSpace(8) = (uint64_t)data;
46 *(uint64_t *)AI_PacketSpace(8) = size;
48 AI_EndPacket();
51 return data;
54 void AC_EXPORT *malloc(size_t size) {
55 if(AC_mallocFP == NULL) {
56 *(void **)&AC_mallocFP = dlsym(RTLD_NEXT, "malloc");
59 void *data = AC_mallocFP(size);
61 if(AI_CollectionStatus()) {
62 AI_StartPacket(AC_moduleID);
64 *(uint8_t *)AI_PacketSpace(1) = 2;
65 *(uint64_t *)AI_PacketSpace(8) = AI_Timestamp();
66 *(uint64_t *)AI_PacketSpace(8) = (uint64_t)data;
67 *(uint64_t *)AI_PacketSpace(8) = size;
69 AI_EndPacket();
72 return data;
75 void AC_EXPORT free(void *ptr) {
76 if(AC_freeFP == NULL) {
77 *(void **)&AC_freeFP = dlsym(RTLD_NEXT, "free");
80 if(AI_CollectionStatus()) {
81 AI_StartPacket(AC_moduleID);
83 *(uint8_t *)AI_PacketSpace(1) = 3;
84 *(uint64_t *)AI_PacketSpace(8) = AI_Timestamp();
85 *(uint64_t *)AI_PacketSpace(8) = (uint64_t)ptr;
87 AI_EndPacket();
90 AC_freeFP(ptr);
93 void AC_EXPORT *realloc(void *ptr, size_t size) {
94 if(AC_reallocFP == NULL) {
95 *(void **)&AC_reallocFP = dlsym(RTLD_NEXT, "realloc");
98 void *data = AC_reallocFP(ptr, size);
99 if(AI_CollectionStatus()) {
100 AI_StartPacket(AC_moduleID);
102 *(uint8_t *)AI_PacketSpace(1) = 4;
103 *(uint64_t *)AI_PacketSpace(8) = AI_Timestamp();
104 *(uint64_t *)AI_PacketSpace(8) = (uint64_t)ptr;
105 *(uint64_t *)AI_PacketSpace(8) = (uint64_t)data;
106 *(uint64_t *)AI_PacketSpace(8) = size;
108 AI_EndPacket();
111 return data;
114 void __attribute__((constructor)) AC_EXPORT AM_Construct() {
115 AI_Construct();
117 AC_moduleID = AI_ConfigurationLong("memory:moduleID");
119 AI_ModuleLoaded("memory", AC_moduleID);
122 void __attribute__((destructor)) AC_EXPORT AM_Destruct() {