C++ and stuff on embedded processors, again

So conventionally the idea is that things like dynamic memory allocation, smart pointers, modern C++ abstraction and templates etc. is too "heavyweight" to use on 8 bit processors.

Not really true at all, here's the output from a smart pointer implementation I wrote using a custom embedded-appropriate memory allocation policy in straight C++11 for the AVR Mega series; this is the output over the hardware serial port from an ATMega328 to a PC terminal:

This is just some structs being allocated dynamically to a memory pool on the heap inside a function, managed by the lightweight smart pointer object on the stack, which then de-allocates the heap memory automatically when the smart pointer object goes out of scope as the function returns. The total codebase size is just under 1k SLOC, which compiles down to using about 5.5 kb of program memory space on the device. SRAM usage is dependent on the size of the pool/type of managed object

Reply to
bitrex
Loading thread data ...
[...]

An OT in the subject line would be appreciated in future, thanks very much.

-- This message may be freely reproduced without limit or charge only via the Usenet protocol. Reproduction in whole or part through other protocols, whether for profit or not, is conditional upon a charge of GBP10.00 per reproduction. Publication in this manner via non-Usenet protocols constitutes acceptance of this condition.

Reply to
Cursitor Doom

Won't that eventually involve some garbage collection?

--

John Larkin         Highland Technology, Inc 

lunatic fringe electronics
Reply to
John Larkin

Why is electronics off topic here? Are you saying he should be posting politics?

--

Rick C 

Viewed the eclipse at Wintercrest Farms, 
on the centerline of totality since 1998
Reply to
rickman

Or heap fragmentation. I'd never use free/delete on a processor that small. Of course in a toy implementation where all the malloc()s are the same size, it ought to be okay, but then one might as well use a static array and manage it oneself.

Cheers

Phil Hobbs

--
Dr Philip C D Hobbs 
Principal Consultant 
ElectroOptical Innovations LLC 
Optics, Electro-optics, Photonics, Analog Electronics 

160 North State Road #203 
Briarcliff Manor NY 10510 

hobbs at electrooptical dot net 
http://electrooptical.net
Reply to
Phil Hobbs

A reference-counting smart pointer is kind of "semi-automated" garbage collection - each instance maintains a reference to a common control block that keeps track of how many active references to the actual heap allocated struct there are, and decrements the count when an particular pointer instance goes out of scope. Each instance also holds a common reference to the allocated object's destructor, so that when the last refcounting pointer goes out of scope and the count hits zero again it "knows" to fire the destructor and free memory automatically without the user needing to call "delete" on the struct explicitly.

Because lil processors like the 8 bit AVR and Cortex M0, etc. only have a small amount of heap and no MMU the standard C++ calls "new" and "delete" aren't used in the implementation, but a custom allocator where a big pool of some user-defined size is created at startup, and then blocks are allocated and freed as appropriate. It uses space in the free blocks themselves to store pointers for the logic to determine the memory address of the next free block and the previous used block, like a linked list.

So there's naturally the limitation that the user has to define somewhere the maximum number of objects a pool can hold and if you go over that the allocation fails. In this implementation it's not done with old C-style macros but templates, so by simply writing say:

struct Foo {

//constructor Foo(int a) { _a = a; }

//structure fields int _a; };

void somefunc() { auto my_ptr1 = allocated_shared(1); auto my_ptr2 = allocated_shared(2); }

the compiler automatically "knows" to create a custom allocator block in SRAM, make it big enough to hold 5 structures of type "Foo", and call Foo's constructor with the argument in parenthesis returning a smart pointer. "my_ptr1" and "my_ptr2" then behaves exactly like a regular C/C++ pointer that you can do all the usual stuff with, except that you don't have to worry about ever explicitly freeing the heap memory the structure is using.

Reply to
bitrex

I wouldn't use the stdlib delete in unmanaged SRAM on a processor that small either.

But there are plenty of use cases where allocating/deallocating many small objects of the same size is just what one wants to do! Yep, you absolutely could manage a static array for them yourself. Also very inflexible and error-prone. And you don't need to, the software can handle it for you.

Reply to
bitrex

Good grief, why?

Looks a lot like "it's more fun to play with fancy tools than code the boring application." I've seen way too much of that.

--

John Larkin         Highland Technology, Inc 

lunatic fringe electronics
Reply to
John Larkin

lol

Reply to
bitrex

In PowerBASIC!

Reply to
bitrex

In any case I have another dev-helper C++ API in the works that folks here might find more practical; a generic LED/LCD display library that you can write text to like an ostream/terminal, and supports hypothetically any type of display by abstracting away the "physical layer" implementation details from the logic by using plug-ins/policies

Reply to
bitrex

It's not a toy, it's a tool. It gets electronics built.

formatting link

formatting link

formatting link

formatting link

formatting link

formatting link

formatting link

formatting link

formatting link

and lots more.

--

John Larkin         Highland Technology, Inc 

lunatic fringe electronics
Reply to
John Larkin

Really? Programming smallish processors is off-topic here?

Cursitor Doom's idea of legitimate subjects for this group does seem to be limited by his imperfect grasp of electronics.

One of the stronger rules in electronics is that almost anything you can do in an analog circuit can be duplicated in a digital circuit. It does seem to take more transistors, but - as Jim pointed out recently - digital trans istors have gotten a lot smaller recently. They can be a lot crummier than analog transistors, which helps.

--
Bill Sloman, Sydney
Reply to
bill.sloman

That's the customers problem :-)

Reply to
Bill Martin

When did any ARM get to be an 8-bit cpu?

Reply to
Bill Martin

The Arduino environment is C++. The library is nonstandard, but all the C++ language features are there. The compiler is gcc.

John Nagle

Reply to
John Nagle

A much more appropriate newsgroup would have been comp.arch.embedded.

Reply to
upsidedown

On a sunny day (Sat, 30 Sep 2017 14:48:38 -0700) it happened John Larkin wrote in :

Fun fun fun

All coded in plain old C (gcc) sizes are in BYTES. All programs run on any Linux PC and also on Raspberries.

formatting link
Navigation app, just added roll an pitch indicator center screen bottom: libraries used: XLOCAL_LIBRARIES = $(XAWLIB) $(XLIB) -ljpeg -lm -ldl -lXt

31268 Sep 9 10:06 /usr/bin/xgpspc_mon main server program that runs all sensors and does GPS processing, navigation, steering, auto-pilot, many things more: 321412 Sep 30 20:52 xgpspc on rapberry

formatting link
Spectrum analyzer, coded in C using libforms, running on raspberry, remote via ssh from AMD PC:

617393 Sep 17 2016 /usr/local/bin/xpsa

formatting link
Newsreader, uses libforms:

2245400 Apr 26 2014 /usr/local/bin/NewsFleX

formatting link
Satellite receiver / recorder/ dish-positioner / calculator, uses libforms

427045 Aug 7 19:18 /usr/local/bin/xdipo

formatting link
Video digitizer control, can also control the cable receiver via IR, uses libforms:

108909 Aug 5 19:26 /usr/bin/xpavrt

formatting link
Color selector and timer for room LED lights, also can do disco lights:

122017 Jan 25 2016 /usr/local/bin/col_pic

formatting link
Audio player equalizer, FFT display, scope, uses libforms:

96865 Apr 17 2016 /usr/bin/xpequ

formatting link
Audio file selector, makes databse of all audio files on system, has search, uses libforms:

61871 Jun 14 18:54 /usr/bin/xmpl

formatting link
Ceefax / 'teletext' viewer, click a page, no libs execpt xlib, calls 'xdipo' to tune to a station:

5371 Jun 15 2016 /usr/bin/xvtx-p

and lots more. All this is open source, not always the latest version on my site... I do keep changing things at times.

Anyone else ??? ;-)

Who needs seeplushplush? FOR WHAT????

Reply to
Jan Panteltje

On a sunny day (Sat, 30 Sep 2017 16:35:20 -0400) it happened Phil Hobbs wrote in :

My programs are full of malloc() and free(). Not a big deal as long as you do not try to free() the same area more than once ;-)

Reply to
Jan Panteltje

Never, apologies if the wording was ambiguous

Reply to
bitrex

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.