On Tue, Oct 28, 2014 at 4:41 PM, Michael Wagner <[email protected]>
wrote:

> 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?
>
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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to