Author Topic: Mathomatic Port to Ndless gives a memory error  (Read 16512 times)

0 Members and 1 Guest are viewing this topic.

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Mathomatic Port to Ndless gives a memory error
« on: April 25, 2014, 06:50:00 pm »
Hello all,

I am trying to port Mathomatic, a computer cas system, to the Nspire CX using ndless. It says it doesn't have enough memory when I run it. I have told it HANDHELD=1 and SECURE=1 so it minimizes memory usage and have taken the number of equation spaces waaay down to the lowest the code says it can go. Also, somewhere in the readme's it says that it can run on a few MB's of RAM, so it should work on the Nspire. Sometimes it crashes the calculator and I have to reset. Frustrating process of resetting, installing ndless, etc....

I have made minimal modifications to the code, and it compiles and links without major warnings.

Any ideas? I can provide more information if you want I just don't know what else to say save telling my entire process (cmd lines, etc.).

Oh, and the code is C.

Quadratic

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #1 on: April 25, 2014, 07:13:49 pm »
The problem is, that although the nspire has 64 MB RAM, you can't get huge continuous chunks of memory.
Are you using os.h or fully relying on newlib? You could insert a message into the malloc implementation in system/for-newlib/stdlib.cpp which tells you the size it's trying to allocate.

Quote
I have made minimal modifications to the code, and it compiles and links without major warnings.
Which warnings are left and what modifications did you do?
Did you use "nspire-gcc" as the compiler or did you use "arm-none-eabi-gcc"? Did you add "-mlong-calls" to CFLAGS?

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #2 on: April 25, 2014, 08:00:24 pm »
I am so amazed at the quick response time. Thank you.

#include <limits.h>
#include <float.h>
#include <math.h>
#include <setjmp.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
etc.

I'm guessing that is newlib. I never included <os.h> in any file in the code.

"insert a message": how? why? Like a debug message?

I used nspire-gcc and do not think I added the -mlong-calls to the command line (I ran the command a while ago and can't revive my history listing...)
As for modifications, I changed DEFAULT_N_TOKENS to 100 instead of whatever it was (in am.h), and I think whatever I needed to do to get rid of errors. And I changed example.c to fit the nspireio2's functions. (I'm using example.c for my testing purposes. Also I'm using the library and linking it to example.o.)

I'm going to re-compile everything now so I can get more precise information for you. Should I add the -mlong-calls to the command line? Should I not use nspire-gcc?

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #3 on: April 25, 2014, 08:11:31 pm »
Quote
"insert a message": how? why? Like a debug message?
Jup, just call puts (printf won't work without changes in nspire-ld-bflt) or set a breakpoint there (or use asm volatile("bkpt"))

Quote
I'm going to re-compile everything now so I can get more precise information for you. Should I add the -mlong-calls to the command line? Should I not use nspire-gcc?
You should. -mlong-calls should also resolve some crashes you noticed.

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #4 on: April 25, 2014, 08:48:37 pm »
Ok. The calc still crashes (freezes up). May be my code, may not. I've checked mine as best I can. Here is the command line:

nspire-gcc  -O3  -Wall -Wshadow -Wno-char-subscripts -marm  -mlong-calls -fexceptions -DVERSION=\"`cat VERSION`\" -DLIBRARY -DSILENT=1 -DSECURE -DHANDHELD -DNO_COLOR     -c -o am.o am.c

The above for every library source code file, then the following for example.c:

nspire-gcc -Wall -marm -Os -c lib/example.c

Then I linked:

 nspire-ld-bflt am.o cmds.o complex.o complex_lib.o diff.o factor.o factor_int.o gcd.o globals.o help.o integrate.o lib/lib.o list.o parse.o poly.o simplify.o solve.o super.o unfactor.o lib/example.o -o lib/MathomaticNdless.tns

And transferred. Ran and the calc said "not enough memory" and froze. Any ideas? Should I post source code? Would rebuilding the code without ANY modifications to the code do anything?

Quadratic

(Still amazed at the quickness of replies...)

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #5 on: April 25, 2014, 09:22:31 pm »
Ok, I fixed the crashes by adding -mlong-calls to the example.c compile command line and by fixing an error in my code (duh, uncomment the exit call after the error reporting "not enough memory" else the code will go on and crash...I should have thought of that waay earlier)

Other than that the code still won't allocate the memory.

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #6 on: April 26, 2014, 07:35:30 am »
Compile everything with "-Og -g" instead of "-O3", "-fno-execptions" instead of "-fexceptions" (What do you want to do with exceptions in C?)
and debug it. If you can, set a breakpoint at malloc or a puts call and find out what it's allocating.

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #7 on: April 26, 2014, 10:21:48 am »
What do I want to do with exceptions in C? I don't know, but the makefile that came with mathomatic apparently had it in.

Debug it: I can't. The emulator requires boot1 and boot2 and polydumper only dumps my boot1 for some reason, which I can't figure out.

And please pardon my apparent stupidity on this:

nspire-gcc  -Og -g -Wall -Wshadow -Wno-char-subscripts -Wno-unused-variable -marm -mlong-calls  -fno-exceptions -DLIBRARY -DVERSION=\"`cat ../VERSION`\" -DSILENT=1 -DSECURE -DHANDHELD    -c -o lib.o lib.c
cc1.exe: error: argument to '-O' should be a non-negative integer
make: *** [lib.o] Error 1

-Og throws the error I expected it too...where am I wrong here?

Oh, and if I compile with arm-none-eabi-gcc, the calc freezes on the document screen right when I click on mathomaticndless.tns. I'm curious: why then and not after it loads the console?
« Last Edit: April 26, 2014, 10:41:36 am by quadratic77 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #8 on: April 26, 2014, 11:59:49 am »
Quote
What do I want to do with exceptions in C? I don't know, but the makefile that came with mathomatic apparently had it in.
Remove it! It doesn't make sense and exceptions will crash the calculator.

Quote
Debug it: I can't. The emulator requires boot1 and boot2 and polydumper only dumps my boot1 for some reason, which I can't figure out.
You can get a boot2 from tiplanet.org and from a OS image (.tcc or .tnc) if you rename it to .zip.

Quote
nspire-gcc  -Og -g -Wall -Wshadow -Wno-char-subscripts -Wno-unused-variable -marm -mlong-calls  -fno-exceptions -DLIBRARY -DVERSION=\"`cat ../VERSION`\" -DSILENT=1 -DSECURE -DHANDHELD    -c -o lib.o lib.c
cc1.exe: error: argument to '-O' should be a non-negative integer
make: *** [lib.o] Error 1

-Og throws the error I expected it too...where am I wrong here?
Hmm, maybe your compiler doesn't support "-Og". "-O0 -g" should work as well, and also set "-g --debug" as flags for nspire-ld-bflt.

Quote
Oh, and if I compile with arm-none-eabi-gcc, the calc freezes on the document screen right when I click on mathomaticndless.tns. I'm curious: why then and not after it loads the console?
Some flags for gcc are missing, in particular "-fpic" so the code will crash after being loaded.

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #9 on: April 26, 2014, 03:55:35 pm »
Ok, tracking it down to this:

((scratch = (token_type *) malloc(((n_tokens * 3) / 2) * sizeof(token_type))) == NULL
        || (tes = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
        || (tlhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
        || (trhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL)

That block returns false. As for malloc, putting a puts into it crashes the calc. The emulator worked fine until I restarted it. Lost my ndless and ndless folder on the emulator and I don't care to re-do everything right now. And I can't find the malloc function. "system" folder in where? Sorry if I'm asking too many questions and not doing the work myself.

And congratulations on reaching the 700 post mark :).
« Last Edit: April 26, 2014, 03:57:06 pm by quadratic77 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #10 on: April 26, 2014, 03:58:24 pm »
n_tokens? Try to set DEFAULT_N_TOKENS even lower, 10 maybe.

Quote
The emulator worked fine until I restarted it.
Did you save the flash?

Quote
And I can't find the malloc function. "system" folder in where?
In your ndless-sdk

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #11 on: April 26, 2014, 05:45:15 pm »
Yes, I saved the flash as nand.img (maybe I saved it in the wrong location, where should I?). And when I loaded the emulator up again, it said to "press I to load default operating system" or something like that. And it did load the default OS again, just like the first time.

Now, if I change DEFAULT_N_TOKENS to 10 and make the "out of bounds" error to <10, it fails at this:

if (n_tokens <= 0){
        return false; }

(in init_mem() ;) -um, that's supposed to be a ';' and a ')', not a smiley...

So it seems like the DEFAULT_N_TOKENS HAS to be at least 100....and, the documentation says mathomatic can run on a few mb's of RAM, so I shouldn't have to hack it down lower.
« Last Edit: April 26, 2014, 05:46:56 pm by quadratic77 »

Offline Vogtinator

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1193
  • Rating: +108/-5
  • Instruction counter
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #12 on: April 26, 2014, 06:25:10 pm »
Quote
So it seems like the DEFAULT_N_TOKENS HAS to be at least 100....and, the documentation says mathomatic can run on a few mb's of RAM, so I shouldn't have to hack it down lower.
Than you need to log what it dynamically allocates, either using gdb or a puts in malloc.
If the allocations aren't that much (<5MB each), something else is wrong.

Offline Legimet

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +29/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #13 on: April 28, 2014, 10:38:43 pm »
Are you using Newlib? Get your Ndless SDK from github.com/OlivierA/Ndless and compile it. You also need to rebuild your toolchain using a script in Ndless-SDK/toolochain (or wait until ExtendeD provides binaries)

Offline quadratic77

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 16
  • Rating: +0/-0
    • View Profile
Re: Mathomatic Port to Ndless gives a memory error
« Reply #14 on: May 07, 2014, 01:01:13 pm »
Instead of putting a breakpoint in malloc I did this:

    if ((scratch = (token_type *) malloc(((n_tokens * 3) / 2) * sizeof(token_type))) == NULL
        || (tes = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
        || (tlhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL
        || (trhs = (token_type *) malloc(n_tokens * sizeof(token_type))) == NULL) {
        return (((n_tokens * 3 )/ 2 ) * sizeof(token_type));
    }

So if that block (the one that is failing) fails, I'll know the largest block size it's allocating. The return value is 2400. Is that what you were asking for?

Quadratic77