Author Topic: [68k] - malloc throws a memory error  (Read 4420 times)

0 Members and 1 Guest are viewing this topic.

Offline Torio

  • LV3 Member (Next: 100)
  • ***
  • Posts: 83
  • Rating: +22/-0
    • View Profile
[68k] - malloc throws a memory error
« on: August 30, 2012, 02:21:03 pm »
Hello,
Another question for you Lionel ! (...or for anybody who knows the answer of course)

This instruction throws a memory error :
Code: [Select]
void *buffer = malloc(LCD_SIZE);
I think that malloc returns only NULL when there is not enough memory, and doesn't throw any error.
Besides, I think that a lack of memory is not a problem because this code prints 3840/78868 before the malloc, so there is enough memory:
Code: [Select]
printf("%d / %lu", LCD_SIZE, HeapAvail());
I know it's vague but I can't post all the code of my fat program here, but perhaps you've encountered a situation like that before me and you could enlighten me ?
Thanks in advance,
Torio
Sorry about my mistakes, I'm French.

Projects :  Pokemon TI-89 | Ti-Tank

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: [68k] - malloc throws a memory error
« Reply #1 on: August 30, 2012, 02:39:55 pm »
Indeed, it's extremely unusual for malloc (HeapAllocPtr) to throw errors...
Browsing through the callgraphs in the documentation, I see that HeapAllocPtr calls NeedStack and HeapAllocHigh, which itself calls NeedStack and several other functions.
I'm positive that the Memory error is thrown by NeedStack - which, in turn, means that you have stored way too much data on your stack. You need to reduce your stack consumption :)
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Torio

  • LV3 Member (Next: 100)
  • ***
  • Posts: 83
  • Rating: +22/-0
    • View Profile
Re: [68k] - malloc throws a memory error
« Reply #2 on: August 30, 2012, 02:58:21 pm »
Well, well, well... Always that bloody stack. :banghead:
So let's review the methods you gave me in a previous subject :

Quote from: Lionel Debroux
eliminating SAVE_STACK (saving/restoring the screen yourself, in an allocated memory block or through the method used by the Home Screen Restore program)
Done.

Quote from: Lionel Debroux
reducing call nesting
I don't use recersive functions.

Quote from: Lionel Debroux
using the registers to pass parameters (__attribute__((__regparm__)) on single functions, -mregparm=5 compiler switch)
This may be my last option.
I don't know what you mean by passing parameters on single functions (and what is a single function, by the way ?)...
For -mregparm=5 compiler switch, I guess I have to add "-mregparm=5" at the end of the text box in Project->Options->Compilation->GCC Switches. Am I right ?
Sorry about my mistakes, I'm French.

Projects :  Pokemon TI-89 | Ti-Tank

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: [68k] - malloc throws a memory error
« Reply #3 on: August 30, 2012, 03:24:34 pm »
Quote
For -mregparm=5 compiler switch, I guess I have to add "-mregparm=5" at the end of the text box in Project->Options->Compilation->GCC Switches. Am I right ?
Yup. And that's a global switch which applies to all functions; if you wanted to enable it on several functions only, you'd use __attribute__((__regparm__(5))) in the definition of each of those functions :)

Are you saving and restoring the screen yourself to local buffers (buffers declared inside functions, as opposed to global buffers) ?

In any case, you can send me a copy of the code base, like the previous time I performed optimizations on it :)
I don't think I'll have time to do much on it before the week-end, though.
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Torio

  • LV3 Member (Next: 100)
  • ***
  • Posts: 83
  • Rating: +22/-0
    • View Profile
Re: [68k] - malloc throws a memory error
« Reply #4 on: August 31, 2012, 03:35:45 am »
Quote from: Lionel Debroux
Are you saving and restoring the screen yourself to local buffers (buffers declared inside functions, as opposed to global buffers) ?
Yes, it's already done  ;)

Quote from: Lionel Debroux
Yup. And that's a global switch which applies to all functions; if you wanted to enable it on several functions only, you'd use __attribute__((__regparm__(5))) in the definition of each of those functions
I applied the switch, it doesn't change anything...  ???

Quote from: Lionel Debroux
In any case, you can send me a copy of the code base, like the previous time I performed optimizations on it
I will send it to you by mail in a few hours.  ;)

Quote from: Lionel Debroux
I don't think I'll have time to do much on it before the week-end, though.
Oh, don't worry about that. Tomorrow is the beginning of school for me, and I take my first year of Prepa (MPSI, if you want to know) so I will only have access to my computer on holiday. You will have about 1-2 months to look my code. Is that okay ?  ;D
Sorry about my mistakes, I'm French.

Projects :  Pokemon TI-89 | Ti-Tank

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: [68k] - malloc throws a memory error
« Reply #5 on: August 31, 2012, 04:01:56 am »
Quote
Quote
Are you saving and restoring the screen yourself to local buffers (buffers declared inside functions, as opposed to global buffers) ?
Yes, it's already done ;)
But precisely, it loads the stack quite a bit ;)

Quote
I applied the switch [-mregparm=5], it doesn't change anything...
That was expected, it was unlikely to save more than, say, several dozens of bytes.

Quote
so I will only have access to my computer on holiday
But you won't have any holidays because you'll have way too much homework related to math and physics, and pretty little about computer science ;)
« Last Edit: August 31, 2012, 04:02:08 am by Lionel Debroux »
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.