-1

I'm using gnu sparc tool chain. I have sparc-ab-elf-gcc and sparc-ab-linux-gcc. ('ab' is the processor name). I want to compile a program which uses malloc and free (this program is supposed to be run on linux) on baremetal(without os). So I should compile it using sparc-ab-elf-gcc. I heard I can use dlmalloc for this case. (see stdlib-like library on bare metal environment? (memory management and hopefully pthread support) ) but I'm seeing some compile error for simple test program which just does malloc and free once each time.

// test malloc, realloc and free
#include <aldebaran.h>
#include <malloc.h>


int main()
{
int i;

ab_printf("%s\n",CKIM_XX);
char *buff = dlmalloc(100);
for(i = 0; i < 100; i++) {
    buff[i] = (char) i;
}
ab_printf("buff = %x\n'");
for(i=0; i<100; i++) {
    ab_printf("%d ", buff[i]);
}
ab_printf("\n");
dlfree(buff);
return 0;
}


ckim@stph45:~/prj2/abts/yolo-bare/darknet] make test3
sparc-ab-elf-gcc -c -I./src -I../include -I/opt/abde/sparc-ab-elf/include    -DALDEBARAN_FPGA -DABC_CT -O3 -ffast-math -fcommon -msoft-float -mcpu=v8  -Wa,-xarch=v8plusb -Wall -Wfatal-errors   ./src/malloc.c -o obj/malloc.o
./src/malloc.c:571:40: fatal error: sys/mman.h: No such file or directory
compilation terminated.
make: *** [obj/malloc.o] Error 1

My bare-metal system doesn't have sys/mman.h (I searched it on the elf toolchain library) so tried again after defining LACKS_SYS_MMAN_H and got

ckim@stph45:~/prj2/abts/yolo-bare/darknet] make test3
sparc-ab-elf-gcc -c -I./src -I../include -I/opt/abde/sparc-ab-elf/include    -DALDEBARAN_FPGA -DABC_CT -O3 -ffast-math -fcommon -msoft-float -mcpu=v8  -Wa,-xarch=v8plusb -Wall -Wfatal-errors  -DLACKS_SYS_MMAN_H  ./src/test3.c -o obj/test3.o
sparc-ab-elf-gcc -c -I./src -I../include -I/opt/abde/sparc-ab-elf/include    -DALDEBARAN_FPGA -DABC_CT -O3 -ffast-math -fcommon -msoft-float -mcpu=v8  -Wa,-xarch=v8plusb -Wall -Wfatal-errors  -DLACKS_SYS_MMAN_H  ./src/malloc.c -o obj/malloc.o
./src/malloc.c: In function 'mmap_alloc':
./src/malloc.c:2924:5: warning: implicit declaration of function 'mmap' [-Wimplicit-function-declaration]
./src/malloc.c:2924:24: error: 'PROT_READ' undeclared (first use in this function)
compilation terminated due to -Wfatal-errors.
make: *** [obj/malloc.o] Error 1

The dlmalloc.c is a very long program. (I divided it into malloc.h and malloc.c) Below is the function making error..

/* Malloc using mmap */
static void* mmap_alloc(mstate m, size_t nb) {
  size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK);
  if (m->footprint_limit != 0) {
    size_t fp = m->footprint + mmsize;
    if (fp <= m->footprint || fp > m->footprint_limit)
      return 0;
  }
  if (mmsize > nb) {     /* Check for wrap around 0 */
    char* mm = (char*)(CALL_DIRECT_MMAP(mmsize));    <=== line causing error.
    if (mm != CMFAIL) {
      size_t offset = align_offset(chunk2mem(mm));
      size_t psize = mmsize - offset - MMAP_FOOT_PAD;
      mchunkptr p = (mchunkptr)(mm + offset);
      p->prev_foot = offset;
      p->head = psize;
      mark_inuse_foot(m, p, psize);
      chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD;
      chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0;

      if (m->least_addr == 0 || mm < m->least_addr)
        m->least_addr = mm;
      if ((m->footprint += mmsize) > m->max_footprint)
        m->max_footprint = m->footprint;
      assert(is_aligned(chunk2mem(p)));
      check_mmapped_chunk(m, p);
      return chunk2mem(p);
    }
  }
  return 0;
}

can somebody give me a light?

Chan Kim
  • 5,177
  • 12
  • 57
  • 112

1 Answers1

0

I heard I can use dlmalloc for this case

Read that answer again. Note in particular: "... you should be able to adapt ...". You haven't done the adapting part.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Yes, I was in the process of adaption when I met with the error. After some thoughts, having decided it seems to be a bigger work than I thought, I turned away from using dlmalloc and doing it another way. (not using malloc). – Chan Kim Jun 09 '17 at 04:27
  • The problem was it seems to have something to do with the toolchain, and I have another old persistig problem in building the toolchain in my system. so I will get out of my hand I thought. – Chan Kim Jun 09 '17 at 04:28