You can combine message offset and message identifier into a single integer (let's call it moi - message offset and id): moi=message_offset*n+message_id, where n is a constant high limit of message identifiers.
Now, to register a new message you should atomically read moi and add new_message_length*n+1 to it. To get offset use moi/n, to get id - moi%n.