- /* delete records if too much memory is in use */
- while (s->curr_string_mem > s->max_string_mem)
- {
- asl_memory_record_clear(s, s->record[s->record_first]);
- s->record_first++;
- if (s->record_first >= s->record_count) s->record_first = 0;
- }
+ /* asl_memory_message_encode creates and caches strings */
+ status = asl_memory_message_encode(s, msg);
+ if (status == ASL_STATUS_OK)
+ {
+ uint32_t loop_start_index = s->record_first;
+
+ if (*mid != 0)
+ {
+ s->buffer_record->mid = *mid;
+ }
+ else
+ {
+ s->buffer_record->mid = asl_core_new_msg_id(0);
+ *mid = s->buffer_record->mid;
+ }
+
+ /* clear the first record */
+ t = s->record[s->record_first];
+ asl_memory_record_clear(s, t);
+
+ /* add the new record to the record list (swap in the buffer record) */
+ s->record[s->record_first] = s->buffer_record;
+ s->buffer_record = t;
+
+ /* record list is a circular queue */
+ s->record_first++;
+ if (s->record_first >= s->record_count) s->record_first = 0;
+
+ /* delete records if too much memory is in use */
+ while (s->curr_string_mem > s->max_string_mem)
+ {
+ asl_memory_record_clear(s, s->record[s->record_first]);
+ s->record_first++;
+ if (s->record_first >= s->record_count) s->record_first = 0;
+ if (s->record_first == loop_start_index)
+ {
+ /* The entire ring has been cleared. This should never happen. */
+ asl_memory_reset(s);
+ status = ASL_STATUS_FAILED;
+ break;
+ }
+ }
+ }
+ });