Identifying a Ring state

51 views
Skip to first unread message

Mansour Ayouni

unread,
Jul 4, 2024, 5:36:24 PMJul 4
to The Ring Programming Language
Hello Mahmoud and Ilir,

I'm writing a function to check if a given variable, whatever type it has, is a valid Ring state currently "alive" in the program.

This is my code where I return TRUE after checking that the variable is a pointer with the value "RINGSTATE" in its second item, but not only that, since I try to run a dummy code in the state (just " ") and see how the ring_state_runcode() function will react...

image.png

Is my implementation correct? Or there may be a better way to do it? Or a plan form Mahmoud to add to its TODO list as a native function in Ring?

All the best,
Mansour

Ilir Liburn

unread,
Jul 4, 2024, 6:49:57 PMJul 4
to The Ring Programming Language
Hello Mansour,

Your code is correct. Running code only if pointer type is RINGSTATE pointer, because any other pointer type could return NULL (in C) which can cause a crash.
Try-catch is also nice addition because ring_state_runcode could fail (while allocating memory or some other failure). 

Greetings,
Ilir

Mansour Ayouni

unread,
Jul 4, 2024, 6:56:22 PMJul 4
to Ilir Liburn, The Ring Programming Language
Hello Ilir,

Thank you for your reply and explanation. Much appreciated.

Best wishes,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ring-lang/1d3153a4-3121-4dc6-8a71-a46797098db0n%40googlegroups.com.

Ilir Liburn

unread,
Jul 4, 2024, 7:10:15 PMJul 4
to The Ring Programming Language
Hello Mansour,

You're Welcome. One correction: I think Ring calls C exit if memory can't be allocated (in case memory pool is exhausted) thus quitting the program.
Maybe Mahmoud could revise this by not calling C exit in case code is embedded, although it is questionable if program can continue to work in this case.

Greetings,
Ilir

Mahmoud Fayed

unread,
Jul 4, 2024, 8:47:44 PMJul 4
to The Ring Programming Language
Hello Mansour

Instead of using pPointer[2] 
It's better to use type(pPointer) which will return RINGSTATE as a string

Greetings,
Mahmoud

Mahmoud Fayed

unread,
Jul 4, 2024, 8:50:04 PMJul 4
to The Ring Programming Language
Hello Ilir

>> "Maybe Mahmoud could revise this by not calling C exit in case code is embedded, although it is questionable if program can continue to work in this case"

This common practice in such a situation (if the requested memory is small) is to terminate the program

Greetings,
Mahmoud  

Ilir Liburn

unread,
Jul 5, 2024, 5:08:02 AMJul 5
to The Ring Programming Language
Hello Mahmoud,

it is a situation in which requested memory can be big in size or bigger than size of the memory pool item. Which means, we can still have available item(s) in the memory pool. Program could (or should?) give the user a chance to save his work (possible scenario) and terminate program gracefully.

Greetings,
Ilir

Mahmoud Fayed

unread,
Jul 5, 2024, 6:16:15 AMJul 5
to The Ring Programming Language
Hello Ilir

>> "Program could (or should?) give the user a chance to save his work (possible scenario) and terminate program gracefully."

This logic should be programmed at the application level 
Custom modifications to Ring VM could be done but will not be a reliable solution

If you looked at other programming languages, like Python, you could read this
niceshot.png


The point is ---> Each application could have a different mix of large/small allocations
Where the memory allocation failure could happen at Large or small blocks
And it could happen (After raising an exception that a memory error happens)
 
Greetings,
Mahmoud

Ilir Liburn

unread,
Jul 5, 2024, 7:08:16 AMJul 5
to The Ring Programming Language
Hello Mahmoud,

The logic cannot be programmed at the application level because of the VM code here (and other related memory functions)


Anyway, it is about (at first) about using ring_state and ring_state_runcode allowing application to continue work and do necessary steps before shutdown.
Later, functionality can be extended elsewhere. Unfortunately, the VM code has changed by moving handling of the memory error from the caller to callee (memory allocation routine) which makes impossible to do this. Something to think about in the future.

Greetings,
Ilir

Mahmoud Fayed

unread,
Jul 5, 2024, 7:36:01 AMJul 5
to The Ring Programming Language
Hello Ilir

>> "The logic cannot be programmed at the application level because of the VM code here (and other related memory functions)"

When I say (the logic can be programmed at the application level) I don't mean (Handling the error after it happens), I mean (Preventing the error before it happens), i.e. the application developer could check in which sections the program allocate large memory, and check the available memory before requesting huge memory) - Some external libraries/extensions could help in this purpose

>> "Unfortunately, the VM code has changed by moving handling of the memory error from the caller to callee (memory allocation routine) which makes impossible to do this"

One of the differences between (software) and (hardware) is that we can change the software at lower costs
So, using (impossible) in this context is just an impression.

Greetings,
Mahmoud

Mansour Ayouni

unread,
Jul 5, 2024, 8:13:04 AMJul 5
to Mahmoud Fayed, The Ring Programming Language

Hello Mahmoud, Ilir,

How can we get the size of memory available for a Ring  program? And how we can we get the size of data inside our program variables?

Best,
Mansour


Ilir Liburn

unread,
Jul 5, 2024, 11:11:18 AMJul 5
to The Ring Programming Language
Hello Mansour,

you need to make a system (dependent) call to get the size of the memory available because available memory can be allocated by many apps, therefore it is dynamic. However, on 32 bit system available memory can be max. 2 GB, unless app is compiled in extended mode (3 GB). Rest (up to 4 GB) is reserved for the kernel.

Regarding data: which data do you mean? Basic type number occupies 8 bytes, but variable takes 3-5 items, plus list. Additionally, variable stores information in the hash table.

String and List will take more memory. String will take initially more memory than list (when memory pool is used) because it also allocates string data. We also have pointers (list of 3 items) pointing to some memory which is of unknown size on the Ring side, unless varptr or space functions are used.

You can only see if memory pool is active by calling ringvm_ismempool. Otherwise, system calls are used to allocate or free memory (this is true also if requested size is  bigger than memory pool item). 

Greetings,
Ilir

Mansour Ayouni

unread,
Jul 5, 2024, 1:38:03 PMJul 5
to ilir...@gmail.com, ring...@googlegroups.com

Hello Ilir,

Now I understand it more clearly.

Thank you a lot.

Mansour


Ilir Liburn

unread,
Jul 5, 2024, 2:06:26 PMJul 5
to The Ring Programming Language
Hello Mansour,

You're Welcome.

Greetings,
Ilir
Reply all
Reply to author
Forward
0 new messages