ADD_TEST(help-models ${CMAKE_BINARY_DIR}/teshsuite/simdag/platforms/basic_parsing_test --help-models)
# teshsuite/xbt
- ADD_TEST(tesh-log-large ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/log_large_test.tesh)
- ADD_TEST(tesh-log-parallel ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/parallel_log_crashtest.tesh)
+ ADD_TEST(xbt-log-large ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/log_large_test.tesh)
+ ADD_TEST(xbt-log-parallel ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/parallel_log_crashtest.tesh)
+ ADD_TEST(xbt-mmalloc ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/xbt/mmalloc.tesh)
# teshsuite/gras/datadesc directory
ADD_TEST(tesh-gras-dd-mem ${CMAKE_BINARY_DIR}/bin/tesh ${TESH_OPTION} --cd ${CMAKE_BINARY_DIR}/teshsuite ${CMAKE_HOME_DIRECTORY}/teshsuite/gras/datadesc/datadesc_mem.tesh)
*/
typedef struct mdesc *xbt_mheap_t;
-/* Allocate SIZE bytes of memory. */
+/* Allocate SIZE bytes of memory (and memset it to 0). */
extern void *mmalloc(xbt_mheap_t md, size_t size);
+/* Allocate SIZE bytes of memory (and don't mess with it) */
+void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size);
+
/* Re-allocate the previously allocated block in void*, making the new block
SIZE bytes long. */
extern void *mrealloc(xbt_mheap_t md, void *ptr, size_t size);
+++ /dev/null
-Things that still need attention:
-
- * Make implementation changes necessary to allow multiple processes
- to use the mmalloc managed region simultaneously. This requires,
- at the minimum, some sort of cooperative locking that ensures that
- only one process at a time is changing any of the mmalloc managed
- data structures (its ok for the mmalloc managed data regions to be
- changed at any time since we don't care about their contents).
-
- * In order to support multiple processes using the mmalloc managed
- region, the malloc descriptor needs to be broken into two parts,
- one part which is specific to the given process and is maintained
- separately on a per process basis, and another part which is common
- to all processes. As an example, the file descriptor is specific
- to a given process, as are the morecore and abortfunc pointers.
- However magic[], the version number, the flags field, etc are
- common to all processes.
* under the terms of the license (GNU LGPL) which comes with this package. */
#include "mmprivate.h"
+#include "xbt/ex.h"
/* Return memory to the heap.
Like `mfree' but don't call a mfree_hook if there is one. */
switch (type) {
case -1: /* Already free */
- fprintf(stderr,"Asked to free a fragment in a block that is already free. I'm puzzled\n");
- abort();
+ UNLOCK(mdp);
+ THROWF(system_error, 0, "Asked to free a fragment in a block that is already free. I'm puzzled\n");
break;
case 0:
/* Set size used in the fragment to 0 */
frag_nb = RESIDUAL(ptr, BLOCKSIZE) >> type;
+
+ if( mdp->heapinfo[block].busy_frag.frag_size[frag_nb] == 0){
+ UNLOCK(mdp);
+ THROWF(system_error, 0, "Asked to free a fragment that is already free. I'm puzzled\n");
+ }
+
mdp->heapinfo[block].busy_frag.frag_size[frag_nb] = 0;
if (mdp->heapinfo[block].busy_frag.nfree ==
}
/* Allocate memory from the heap. */
-
-void *mmalloc(xbt_mheap_t mdp, size_t size)
+void *mmalloc(xbt_mheap_t mdp, size_t size) {
+ void *res= mmalloc_no_memset(mdp,size);
+ memset(res,0,size);
+ return res;
+}
+/* Spliting mmalloc this way is mandated by a trick in mrealloc, that gives
+ back the memory of big blocks to the system before reallocating them: we don't
+ want to loose the beginning of the area when this happens */
+void *mmalloc_no_memset(xbt_mheap_t mdp, size_t size)
{
void *result;
size_t block, blocks, lastblocks, start;
mdp -> heapstats.chunks_free--;
mdp -> heapstats.bytes_free -= 1 << log;
- memset(result, 0, requested_size);
-
} else {
/* No free fragments of the desired size, so get a new block
and break it into fragments, returning the first. */
//printf("(%s) No free fragment...",xbt_thread_self_name());
result = mmalloc(mdp, BLOCKSIZE); // does not return NULL
- memset(result, 0, requested_size);
/* Link all fragments but the first into the free list, and mark their requested size to 0. */
block = BLOCK(result);
continue;
}
result = register_morecore(mdp, blocks * BLOCKSIZE);
- memset(result, 0, requested_size);
block = BLOCK(result);
for (it=0;it<blocks;it++){
mdp -> heapstats.bytes_used += blocks * BLOCKSIZE;
mdp -> heapstats.bytes_free -= blocks * BLOCKSIZE;
- memset(result, 0, requested_size);
}
//printf("(%s) Done mallocing. Result is %p\n",xbt_thread_self_name(),result);fflush(stdout);
return (result);
} else {
/* Won't fit, so allocate a new region that will.
- Free the old region first in case there is sufficient
- adjacent free space to grow without moving. */
+ Free the old region first in case there is sufficient adjacent free space to grow without moving.
+ This trick mandates using a specific version of mmalloc that does not memset the memory to 0 after
+ action for obvious reasons. */
blocks = mdp->heapinfo[block].busy_block.size;
/* Prevent free from actually returning memory to the system. */
oldlimit = mdp->heaplimit;
mfree(mdp, ptr);
mdp->heaplimit = oldlimit;
- result = mmalloc(mdp, requested_size);
+ result = mmalloc_no_memset(mdp, requested_size);
if (ptr != result)
memmove(result, ptr, blocks * BLOCKSIZE);
+ /* FIXME: we should memset the end of the recently area */
}
break;
+++ /dev/null
-#include "../mmalloc.h"
-#include <stdio.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-
-#define BUFFSIZE 204800
-#define TESTSIZE 100
-
-int main()
-{
- void *A, *B;
- int fd1, fd2;
- void *heapA, *heapB;
- void *pointers[TESTSIZE];
-/*
- unlink("heap1");
- fd1=open("heap1",O_CREAT|O_RDWR,S_IRWXU|S_IRWXG|S_IRWXO);
- assert(fd1>0);
- close(fd1);
- fd1=open("heap1",O_RDWR);
- assert(fd1>0);
- */
-
- heapA = xbt_mheap_new(-1, sbrk(0) + BUFFSIZE);
- if (heapA == NULL) {
- perror("attach 1 failed");
- fprintf(stderr, "bye\n");
- exit(1);
- }
-
- fprintf(stderr, "HeapA=%p\n", heapA);
-
- int i, size;
- for (i = 0; i < TESTSIZE; i++) {
- size = rand() % 1000;
- pointers[i] = mmalloc(heapA, size);
- fprintf(stderr, "%d bytes allocated at %p\n", size, pointers[i]);
- }
- char c;
- scanf("%c", &c);
-
- for (i = 0; i < TESTSIZE; i++) {
- mfree(heapA, pointers[i]);
- }
-
- fprintf(stderr, "Ok bye bye\n");
- return 0;
-}
add_executable(log_large_test log_large_test.c)
add_executable(parallel_log_crashtest parallel_log_crashtest.c)
+add_executable(mmalloc_test mmalloc_test.c)
### Add definitions for compile
if(NOT WIN32)
target_link_libraries(log_large_test gras m pthread )
target_link_libraries(parallel_log_crashtest gras m pthread )
+ target_link_libraries(mmalloc_test gras m pthread )
else(NOT WIN32)
target_link_libraries(log_large_test gras)
target_link_libraries(parallel_log_crashtest gras)
+ target_link_libraries(mmalloc_test gras)
endif(NOT WIN32)
set(tesh_files
--- /dev/null
+$ ./xbt/mmalloc_test --log=root.fmt:%m%n
+> Allocating a new heap
+> HeapA allocated
+> 100 bytes allocated with offset 1503232
+> 200 bytes allocated with offset 1507072
+> 300 bytes allocated with offset 1507328
+> 400 bytes allocated with offset 1510912
+> 500 bytes allocated with offset 1510400
+> 600 bytes allocated with offset 1511424
+> 700 bytes allocated with offset 1514496
+> 800 bytes allocated with offset 1513472
+> 900 bytes allocated with offset 1512448
+> 1000 bytes allocated with offset 1515520
+> 100 bytes allocated with offset 1506816
+> 200 bytes allocated with offset 1506560
+> 300 bytes allocated with offset 1509888
+> 400 bytes allocated with offset 1509376
+> 500 bytes allocated with offset 1508864
+> 600 bytes allocated with offset 1518592
+> 700 bytes allocated with offset 1517568
+> 800 bytes allocated with offset 1516544
+> 900 bytes allocated with offset 1519616
+> 1000 bytes allocated with offset 1522688
+> 100 bytes allocated with offset 1506304
+> 200 bytes allocated with offset 1506048
+> 300 bytes allocated with offset 1508352
+> 400 bytes allocated with offset 1507840
+> 500 bytes allocated with offset 1523712
+> 600 bytes allocated with offset 1521664
+> 700 bytes allocated with offset 1520640
+> 800 bytes allocated with offset 1527808
+> 900 bytes allocated with offset 1530880
+> 1000 bytes allocated with offset 1529856
+> 100 bytes allocated with offset 1505792
+> 200 bytes allocated with offset 1505536
+> 300 bytes allocated with offset 1527296
+> 400 bytes allocated with offset 1526784
+> 500 bytes allocated with offset 1526272
+> 600 bytes allocated with offset 1528832
+> 700 bytes allocated with offset 1531904
+> 800 bytes allocated with offset 1534976
+> 900 bytes allocated with offset 1533952
+> 1000 bytes allocated with offset 1532928
+> 100 bytes allocated with offset 1505280
+> 200 bytes allocated with offset 1505024
+> 300 bytes allocated with offset 1525760
+> 400 bytes allocated with offset 1525248
+> 500 bytes allocated with offset 1524736
+> 600 bytes allocated with offset 1536000
+> 700 bytes allocated with offset 1539072
+> 800 bytes allocated with offset 1538048
+> 900 bytes allocated with offset 1537024
+> 1000 bytes allocated with offset 1540096
+> 100 bytes allocated with offset 1504768
+> 200 bytes allocated with offset 1504512
+> 300 bytes allocated with offset 1524224
+> 400 bytes allocated with offset 1544192
+> 500 bytes allocated with offset 1547776
+> 600 bytes allocated with offset 1543168
+> 700 bytes allocated with offset 1542144
+> 800 bytes allocated with offset 1541120
+> 900 bytes allocated with offset 1548288
+> 1000 bytes allocated with offset 1551360
+> 100 bytes allocated with offset 1504256
+> 200 bytes allocated with offset 1504000
+> 300 bytes allocated with offset 1547264
+> 400 bytes allocated with offset 1546752
+> 500 bytes allocated with offset 1546240
+> 600 bytes allocated with offset 1550336
+> 700 bytes allocated with offset 1549312
+> 800 bytes allocated with offset 1552384
+> 900 bytes allocated with offset 1555456
+> 1000 bytes allocated with offset 1554432
+> 100 bytes allocated with offset 1503744
+> 200 bytes allocated with offset 1503488
+> 300 bytes allocated with offset 1545728
+> 400 bytes allocated with offset 1545216
+> 500 bytes allocated with offset 1544704
+> 600 bytes allocated with offset 1553408
+> 700 bytes allocated with offset 1556480
+> 800 bytes allocated with offset 1559552
+> 900 bytes allocated with offset 1558528
+> 1000 bytes allocated with offset 1557504
+> 100 bytes allocated with offset 1560576
+> 200 bytes allocated with offset 1564416
+> 300 bytes allocated with offset 1564672
+> 400 bytes allocated with offset 1568256
+> 500 bytes allocated with offset 1567744
+> 600 bytes allocated with offset 1568768
+> 700 bytes allocated with offset 1571840
+> 800 bytes allocated with offset 1570816
+> 900 bytes allocated with offset 1569792
+> 1000 bytes allocated with offset 1572864
+> 100 bytes allocated with offset 1564160
+> 200 bytes allocated with offset 1563904
+> 300 bytes allocated with offset 1567232
+> 400 bytes allocated with offset 1566720
+> 500 bytes allocated with offset 1566208
+> 600 bytes allocated with offset 1575936
+> 700 bytes allocated with offset 1574912
+> 800 bytes allocated with offset 1573888
+> 900 bytes allocated with offset 1576960
+> 1000 bytes allocated with offset 1580032
+> Done; bye bye
+
--- /dev/null
+#include "xbt/mmalloc.h"
+#include "xbt.h"
+#include <stdio.h>
+#include <assert.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(test,"this test");
+
+#define BUFFSIZE 204800
+#define TESTSIZE 100
+
+int main(int argc, char**argv)
+{
+ void *heapA;
+ void *pointers[TESTSIZE];
+ xbt_init(&argc,argv);
+
+ XBT_INFO("Allocating a new heap");
+ heapA = xbt_mheap_new(-1, ((char*)sbrk(0)) + BUFFSIZE);
+ if (heapA == NULL) {
+ perror("attach 1 failed");
+ fprintf(stderr, "bye\n");
+ exit(1);
+ }
+
+ XBT_INFO("HeapA allocated");
+
+ int i, size;
+ for (i = 0; i < TESTSIZE; i++) {
+ size = ((i % 10)+1)* 100;
+ pointers[i] = mmalloc(heapA, size);
+ XBT_INFO("%d bytes allocated with offset %lu", size, ((char*)pointers[i])-((char*)heapA));
+ }
+
+ for (i = 0; i < TESTSIZE; i++) {
+ xbt_ex_t e;
+ int gotit = 1;
+
+ mfree(heapA, pointers[i]);
+ TRY {
+ mfree(heapA, pointers[i]);
+ gotit = 0;
+ } CATCH(e) {
+ xbt_ex_free(e);
+ }
+ if (!gotit)
+ xbt_die("FAIL: A double-free went undetected (for size:%d)",((i%10)+1)*100);
+ }
+
+ XBT_INFO("Done; bye bye");
+ return 0;
+}
rctx->is_empty = 0;
xbt_strbuff_append(rctx->output_wanted, line);
xbt_strbuff_append(rctx->output_wanted, "\n");
+ XBT_DEBUG("wanted:%s",rctx->output_wanted->data);
break;
case '!':