Is there a C++ placement new() operator for messages?

511 views
Skip to first unread message

Michael Wagner

unread,
Oct 28, 2014, 7:41:28 PM10/28/14
to prot...@googlegroups.com
I would like to use Google Protocol Buffers in a memory constrained environment - not so much that the environment is low on memory as much as memory usage is tracked on a per subsystem basis.
 
Generally, in this environment for other non-Google Protocol Buffers, I use the C++ placement new operator, which allows me to allocate a buffer from a specializes allocator and pass the address of that buffer to the new() operator.
 
char* fooAllocation = memoryBroker->Allocate(MY_SUBSYSTEM_ID, MAX_FOO_SIZE);
 
class Foo foo* = new (fooAllocation);
 
When I am done with foo, I can call
 
memoryBroker->Free(MY_SUBYSTEM_ID, MAX_FOO_SIZE, foo);
 
That allows memoryBroker to keep accurate stats on memory usage by subsystem.
 
I'd like to do the same with a GPB message.
 
Is there some way to do that?
 
Mike

Feng Xiao

unread,
Oct 29, 2014, 2:46:16 AM10/29/14
to Michael Wagner, Protocol Buffers
Not possible with the current implementation but the next version of protobuf will include arena support and with that you will be able to allocate a message and all its sub-fields on an arena. For example:
void ProcessData(const string& data) {
  google::protobuf::ArenaOptions options;
  options.initial_block = memoryBroker->Allocate(MY_SUBSYSTEM_ID, MAX_FOO_SIZE);
  options.initial_block_size = MAX_FOO_SIZE;
  options.max_block_size = 0;  // prevent arena to allocate heap memory by itself.
  {
    google::protobuf::Arena arena(options);
    MyMessage* message = google::protobuf::Arena::CreateMessage<MyMessage>(&arena);
    if (message->ParseFromString(data)) {
      // use message...
    }
    cout << "Memory used for protobuf message: " << arena.SpaceUsed() << endl;
  }
  memoryBroker->Free(MY_SUBSYSTEM_ID, MAX_FOO_SIZE, options.initial_block);
}


 
 
Mike

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages