-1

I have a scenario where I need to create different objects in each iteration of a 'for' loop. The catch here is the synthesizer I am working does not support the "new" keyword. The Synthesizer I am using translates C/C++ code to RTL code (Hardware). So many of the constructs in C++ is not supported by the compiler.

I want to implement something like this:

test inst[5];

for(int i=0;i<5;i++)

   inst[i].test_func();

I googled this problem, but all the solutions i have come across use "new" keyword. I need a way to create different objects on every iteration of the loop without the "new" keyword. Is there a way to do so?

Essentially I am trying to emulate the behavior of 'For-generate' construct in VHDL. Any help or suggestions is greatly appreciated.

Paebbels
  • 15,573
  • 13
  • 70
  • 139
  • There is no C/C++. And `new` is not a keyword. Are you thinking that [tag:c] and [tag:c++] are the same languages? What is the compiler you are talking about? – Iharob Al Asimi Jan 12 '16 at 17:08
  • Your system doesn't support *any* kind of dynamic allocation? Do you know in advance how many (or an upper limit) objects you will need? – BoBTFish Jan 12 '16 at 17:08
  • _"The catch here is the compiler I am working does not support the "new" keyword."_ Huh? Are you sure it's your compiler, or just the target environment that doesn't provide a proper implementation for `new`. Every c++ compiler should accept `new`. – πάντα ῥεῖ Jan 12 '16 at 17:09
  • 6
    @iharob [`new` *is* a keyword](http://en.cppreference.com/w/cpp/keyword/new). But other than that, yes. – BoBTFish Jan 12 '16 at 17:09
  • 1
    global `::operator new` is replaceable so you could implement your own memory pool and still utilize the `new` operator. Can you provide some more detail about what you are trying to do? Possibly include some actual code as well. – Joel Cornett Jan 12 '16 at 17:10
  • But it's more than just a keyword, it's an operator. – Iharob Al Asimi Jan 12 '16 at 17:10
  • What do you intend to do with the objects outside the loop? If nothing, then perhaps they could be stack based instead. Otherwise some kind of allocator may be called for – doctorlove Jan 12 '16 at 17:14
  • In VHDL (another language that synthesizes to hardware), this would be the difference between a FOR...LOOP and a FOR-..GENERATE. Is the upper bound of your for loop fixed, the way a VHDL GENERATE would require? – Ben Voigt Jan 12 '16 at 17:14
  • Maybe you can better show us what you're trying to do by showing us how you would write it with `new`. For instance, when you say "different objects", do you mean "objects of different classes"? – John Sensebe Jan 12 '16 at 17:14
  • 1
    @doctorlove: There is no stack either... he's not using C++ but something more akin to SystemC, and he has a synthesizer, not a compiler. – Ben Voigt Jan 12 '16 at 17:15
  • @πάνταῥεῖ: It's a synthesis tool, not a compiler. The result is not any sort of instruction stream. – Ben Voigt Jan 12 '16 at 17:16
  • I do have a synthesizer, not a compiler. I apologize for not being clear. And I am trying to emulate the behavior of 'For-generate' in VHDL and the upper limit of the loop is fixed. – Nikhil Pratap Jan 12 '16 at 17:26
  • I have edited the question to better reflect my problem. – Nikhil Pratap Jan 12 '16 at 17:34
  • Vivado HLS tool from Xilinx – Nikhil Pratap Jan 12 '16 at 19:47
  • 1
    First, I would suggest to write a new question and mark this one for close/delete. Your tags attracted mostly C/C++ programmers, but you need **HLS developers** (HLS = High-Level-Synthesis). Second, are you realy using C++ or is it just plain C? You have no `new`, no classes no malloc in HLS ... Third, you are creating *hardware*. Hardware has no dynamic memory! All is fixed. So you need to think of fixed sized buffers in HLS, because they can be translated into registers or BlockRAMs. – Paebbels Jan 12 '16 at 21:00
  • What a lot of comments. I would just like to point out that a synthesiser *is* a compiler. That's obvious in this case, because the OP has a compiler that translates something to RTL. In general, the compiler outputs a netlist, which is still a compiled translation. The compiler statements above are actually referring to simulators. – EML Jan 13 '16 at 16:59

6 Answers6

1

If you can't allocate memory dynamically, you'd have to resort to redefining operator new and new[] to use memory from statically allocated pool. You will also have to implement operator delete and delete[] as well. Quite a daunting task, I'd say, unless you have something to relax some requirements for such allocators in general.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
1

I have a suspicion you may be better off forgetting about strange subsets of C++ as a means of generating hardware, and simply writing what you want in VHDL, which, being a hardware description language, has the tools for the job.

While VHDL supports new for simulation, naturally new cannot be used for synthesis, as it implies the dynamic allocation of hardware resources ... not supported by any ASIC or FPGA toolchain in existence today.

So as far as I can see, you simply want an array of 488 objects of whatever type test is, and to operate on all of them simultaneously with the test_func() operation (whatever that is). For which you probably want a for ... generate statement.

0

I'm not sure if this is what you are looking for, but you could do something like this:

class Test {};
class Test0 : public Test {};
class Test1 : public Test {};
class Test2 : public Test {};
class Test3 : public Test {};
class Test4 : public Test {};

static Test0 test0;
static Test1 test1;
static Test2 test2;
static Test3 test3;
static Test4 test4;

int main(int, char **)
{
   Test * tests[5] = {&test0, &test1, &test2, &test3, &test4};

   for (int i=0; i<5; i++)
   {
      Test * t = tests[i];
      // t->init_func();  // or etc
   }
   return 0;
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

You could have all objects preallocated and reusable. I mean, suppose you know you will only need at most 10 objects living concurrently. You then create 10 objects and push them to a list of unused objects

Whenever you need to "create" an object, just take it from the unused objects list. When you no longer need it, you can push it back to that list.

Adi Levin
  • 5,165
  • 1
  • 17
  • 26
  • Thank you for your answer. But the issue is I need to create 488 objects. So I cannot do it line by line. Hence I was looking at using a loop of some sort. – Nikhil Pratap Jan 12 '16 at 19:49
0

If you know the constant size of each object, you could just allocate an array of chars, and then when you need object #i, take the pointer.

int const size_of_obj_in_bytes = 20;
int const num_of_objects_to_allocate = 488;
char c[const num_of_objects_to_allocate*size_of_obj_in_bytes];

obj* get_ptr_to_obj_at_index(int i) {
   return (obj*)(&c[i*size_of_obj_in_bytes]);
}
Adi Levin
  • 5,165
  • 1
  • 17
  • 26
0

if the object is to live in the context of a function, you might be able to utilize stack allocation (alloca) to handle it. Stack allocations should be supported in your subset. You can override the 'new' method to use this function (or whatever is available for stack manipulations).

Just remember, as soon as you leave the parent function, all will be destroyed. You will need to take extra care to call a destructor, if needed.

Serge
  • 11,616
  • 3
  • 18
  • 28