2 * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #include "asl_mini_memory.h"
29 #include <sys/errno.h>
31 #include <sys/types.h>
33 #include <asl_private.h>
36 #define DEFAULT_MAX_RECORDS 256
37 #define MEM_STRING_HEADER_SIZE 8
38 #define CFLOG_LOCAL_TIME_KEY "CFLog Local Time"
39 #define CFLOG_THREAD_KEY "CFLog Thread"
41 #define forever for(;;)
42 extern time_t asl_parse_time(const char *str
);
43 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
46 asl_mini_memory_statistics(asl_mini_memory_t
*s
, aslmsg
*msg
)
53 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
54 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
56 out
= asl_new(ASL_TYPE_MSG
);
57 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
59 size
= sizeof(asl_mini_memory_t
);
60 size
+= ((s
->record_count
+ 1) * sizeof(mini_mem_record_t
));
62 for (i
= 0; i
< s
->string_count
; i
++)
64 size
+= MEM_STRING_HEADER_SIZE
;
65 if (((mini_mem_string_t
*)s
->string_cache
[i
])->str
!= NULL
) size
+= (strlen(((mini_mem_string_t
*)s
->string_cache
[i
])->str
) + 1);
68 snprintf(str
, sizeof(str
), "%llu", size
);
69 asl_set(out
, "Size", str
);
72 for (i
= 0; i
< s
->record_count
; i
++) if (s
->record
[i
]->mid
!= 0) n
++;
74 snprintf(str
, sizeof(str
), "%u", n
);
75 asl_set(out
, "RecordCount", str
);
77 snprintf(str
, sizeof(str
), "%u", s
->string_count
);
78 asl_set(out
, "StringCount", str
);
85 asl_mini_memory_close(asl_mini_memory_t
*s
)
89 if (s
== NULL
) return ASL_STATUS_OK
;
91 if (s
->record
!= NULL
)
93 for (i
= 0; i
< s
->record_count
; i
++)
95 if (s
->record
[i
] != NULL
) free(s
->record
[i
]);
103 if (s
->buffer_record
!= NULL
) free(s
->buffer_record
);
105 if (s
->string_cache
!= NULL
)
107 for (i
= 0; i
< s
->string_count
; i
++)
109 if (s
->string_cache
[i
] != NULL
) free(s
->string_cache
[i
]);
110 s
->string_cache
[i
] = NULL
;
113 free(s
->string_cache
);
114 s
->string_cache
= NULL
;
119 return ASL_STATUS_OK
;
123 asl_mini_memory_open(uint32_t max_records
, asl_mini_memory_t
**s
)
125 asl_mini_memory_t
*out
;
128 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
130 if (max_records
== 0) max_records
= DEFAULT_MAX_RECORDS
;
132 out
= calloc(1, sizeof(asl_mini_memory_t
));
133 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
135 out
->record_count
= max_records
;
136 out
->record
= (mini_mem_record_t
**)calloc(max_records
, sizeof(mini_mem_record_t
*));
137 if (out
->record
== NULL
)
140 return ASL_STATUS_NO_MEMORY
;
143 for (i
= 0; i
< max_records
; i
++)
145 out
->record
[i
] = (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
146 if (out
->record
[i
] == NULL
)
148 asl_mini_memory_close(out
);
149 return ASL_STATUS_NO_MEMORY
;
153 out
->buffer_record
= (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
154 if (out
->buffer_record
== NULL
)
156 asl_mini_memory_close(out
);
157 return ASL_STATUS_NO_MEMORY
;
163 return ASL_STATUS_OK
;
166 static mini_mem_string_t
*
167 mem_string_new(const char *str
, uint32_t len
, uint32_t hash
)
169 mini_mem_string_t
*out
;
172 if (str
== NULL
) return NULL
;
174 ss
= MEM_STRING_HEADER_SIZE
+ len
+ 1;
175 out
= (mini_mem_string_t
*)calloc(1, ss
);
176 if (out
== NULL
) return NULL
;
180 memcpy(out
->str
, str
, len
);
186 * Find the first hash greater than or equal to a given hash in the string cache.
187 * Return s->string_count if hash is greater that or equal to last hash in the string cache.
188 * Caller must check if the hashes match or not.
190 * This routine is used both to find strings in the cache and to determine where to insert
191 * new strings. Note that the caller needs to do extra work after calling this routine.
194 asl_mini_memory_string_cache_search_hash(asl_mini_memory_t
*s
, uint32_t hash
)
196 uint32_t top
, bot
, mid
, range
;
197 mini_mem_string_t
*ms
;
199 if (s
->string_count
== 0) return 0;
200 if (s
->string_count
== 1)
202 ms
= (mini_mem_string_t
*)s
->string_cache
[0];
203 if (hash
< ms
->hash
) return 0;
207 range
= top
= s
->string_count
- 1;
213 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
];
215 if (hash
== ms
->hash
)
219 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
- 1];
220 if (hash
!= ms
->hash
) break;
228 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
];
229 if (hash
< ms
->hash
) top
= mid
;
234 mid
= bot
+ (range
/ 2);
237 ms
= (mini_mem_string_t
*)s
->string_cache
[bot
];
238 if (hash
<= ms
->hash
) return bot
;
240 ms
= (mini_mem_string_t
*)s
->string_cache
[top
];
241 if (hash
<= ms
->hash
) return top
;
243 return s
->string_count
;
247 * Search the string cache.
248 * If the string is in the cache, increment refcount and return it.
249 * If the string is not in cache and create flag is on, create a new string.
250 * Otherwise, return NULL.
252 static mini_mem_string_t
*
253 asl_mini_memory_string_retain(asl_mini_memory_t
*s
, const char *str
, int create
)
255 uint32_t i
, where
, hash
, len
;
257 if (s
== NULL
) return NULL
;
258 if (str
== NULL
) return NULL
;
261 /* check the cache */
262 hash
= asl_core_string_hash(str
, len
);
263 where
= asl_mini_memory_string_cache_search_hash(s
, hash
);
265 /* asl_mini_memory_string_cache_search_hash just tells us where to look */
266 if (where
< s
->string_count
)
268 while (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
== hash
)
270 if (!strcmp(str
, ((mini_mem_string_t
*)(s
->string_cache
[where
]))->str
))
272 ((mini_mem_string_t
*)(s
->string_cache
[where
]))->refcount
++;
273 return s
->string_cache
[where
];
281 if (create
== 0) return NULL
;
283 /* create a new mini_mem_string_t and insert into the cache at index 'where' */
284 if (s
->string_count
== 0)
286 s
->string_cache
= (void **)calloc(1, sizeof(void *));
290 s
->string_cache
= (void **)reallocf(s
->string_cache
, (s
->string_count
+ 1) * sizeof(void *));
291 for (i
= s
->string_count
; i
> where
; i
--) s
->string_cache
[i
] = s
->string_cache
[i
- 1];
294 if (s
->string_cache
== NULL
)
301 s
->string_cache
[where
] = mem_string_new(str
, len
, hash
);
303 return s
->string_cache
[where
];
307 asl_mini_memory_string_release(asl_mini_memory_t
*s
, mini_mem_string_t
*m
)
311 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
312 if (m
== NULL
) return ASL_STATUS_OK
;
314 if (m
->refcount
> 0) m
->refcount
--;
315 if (m
->refcount
> 0) return ASL_STATUS_OK
;
317 where
= asl_mini_memory_string_cache_search_hash(s
, m
->hash
);
318 if (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
320 while (s
->string_cache
[where
] != m
)
322 if (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
325 if (where
>= s
->string_count
) return ASL_STATUS_OK
;
328 for (i
= where
+ 1; i
< s
->string_count
; i
++) s
->string_cache
[i
- 1] = s
->string_cache
[i
];
333 if (s
->string_count
== 0)
335 free(s
->string_cache
);
336 s
->string_cache
= NULL
;
337 return ASL_STATUS_OK
;
340 s
->string_cache
= (void **)reallocf(s
->string_cache
, s
->string_count
* sizeof(void *));
341 if (s
->string_cache
== NULL
)
344 return ASL_STATUS_NO_MEMORY
;
347 return ASL_STATUS_OK
;
351 * Release all a record's strings and reset it's values
354 asl_mini_memory_record_clear(asl_mini_memory_t
*s
, mini_mem_record_t
*r
)
358 if (s
== NULL
) return;
359 if (r
== NULL
) return;
361 asl_mini_memory_string_release(s
, r
->sender
);
362 asl_mini_memory_string_release(s
, r
->facility
);
363 asl_mini_memory_string_release(s
, r
->message
);
365 for (i
= 0; i
< r
->kvcount
; i
++) asl_mini_memory_string_release(s
, r
->kvlist
[i
]);
367 if (r
->kvlist
!= NULL
) free(r
->kvlist
);
368 memset(r
, 0, sizeof(mini_mem_record_t
));
372 asl_mini_memory_record_free(asl_mini_memory_t
*s
, mini_mem_record_t
*r
)
374 asl_mini_memory_record_clear(s
, r
);
379 * Encode an aslmsg as a record structure.
380 * Creates and caches strings.
383 asl_mini_memory_message_encode(asl_mini_memory_t
*s
, aslmsg msg
)
386 mini_mem_string_t
*k
, *v
;
387 mini_mem_record_t
*r
;
388 const char *key
, *val
;
390 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
391 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
392 if (msg
== NULL
) return ASL_STATUS_INVALID_MESSAGE
;
394 r
= s
->buffer_record
;
396 memset(r
, 0, sizeof(mini_mem_record_t
));
399 r
->level
= ASL_LEVEL_DEBUG
;
401 r
->time
= (uint64_t)-1;
402 r
->nano
= (uint32_t)-1;
407 for (x
= asl_msg_fetch((asl_msg_t
*)msg
, 0, &key
, &val
, NULL
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)msg
, x
, &key
, &val
, NULL
))
409 if (key
== NULL
) continue;
411 else if (!strcmp(key
, ASL_KEY_TIME
))
413 if (val
!= NULL
) r
->time
= asl_parse_time(val
);
415 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
417 if (val
!= NULL
) r
->nano
= atoi(val
);
419 else if (!strcmp(key
, ASL_KEY_SENDER
))
421 if (val
!= NULL
) r
->sender
= asl_mini_memory_string_retain(s
, val
, 1);
423 else if (!strcmp(key
, ASL_KEY_PID
))
425 if (val
!= NULL
) r
->pid
= atoi(val
);
427 else if (!strcmp(key
, ASL_KEY_LEVEL
))
429 if (val
!= NULL
) r
->level
= atoi(val
);
431 else if (!strcmp(key
, ASL_KEY_MSG
))
433 if (val
!= NULL
) r
->message
= asl_mini_memory_string_retain(s
, val
, 1);
435 else if (!strcmp(key
, ASL_KEY_FACILITY
))
437 if (val
!= NULL
) r
->facility
= asl_mini_memory_string_retain(s
, val
, 1);
439 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
444 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
449 else if (!strcmp(key
, ASL_KEY_HOST
))
454 else if (!strcmp(key
, ASL_KEY_REF_PID
))
459 else if (!strcmp(key
, ASL_KEY_REF_PROC
))
464 else if (!strcmp(key
, ASL_KEY_SESSION
))
469 else if (!strcmp(key
, ASL_KEY_UID
))
474 else if (!strcmp(key
, ASL_KEY_GID
))
479 else if (!strcmp(key
, ASL_KEY_READ_UID
))
484 else if (!strcmp(key
, ASL_KEY_READ_GID
))
489 else if (!strcmp(key
, CFLOG_LOCAL_TIME_KEY
))
494 else if (!strcmp(key
, CFLOG_THREAD_KEY
))
501 k
= asl_mini_memory_string_retain(s
, key
, 1);
502 if (k
== NULL
) continue;
505 if (val
!= NULL
) v
= asl_mini_memory_string_retain(s
, val
, 1);
509 r
->kvlist
= (mini_mem_string_t
**)calloc(2, sizeof(mini_mem_string_t
*));
513 r
->kvlist
= (mini_mem_string_t
**)realloc(r
->kvlist
, (r
->kvcount
+ 2) * sizeof(mini_mem_string_t
*));
516 if (r
->kvlist
== NULL
)
518 asl_mini_memory_record_clear(s
, r
);
519 return ASL_STATUS_NO_MEMORY
;
522 r
->kvlist
[r
->kvcount
++] = k
;
523 r
->kvlist
[r
->kvcount
++] = v
;
527 return ASL_STATUS_OK
;
531 asl_mini_memory_save(asl_mini_memory_t
*s
, aslmsg msg
, uint64_t *mid
)
534 mini_mem_record_t
*t
;
536 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
537 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
539 /* asl_mini_memory_message_encode creates and caches strings */
540 status
= asl_mini_memory_message_encode(s
, msg
);
541 if (status
!= ASL_STATUS_OK
) return status
;
543 s
->buffer_record
->mid
= s
->next_id
;
546 /* clear the first record */
547 t
= s
->record
[s
->record_first
];
548 asl_mini_memory_record_clear(s
, t
);
550 /* add the new record to the record list (swap in the buffer record) */
551 s
->record
[s
->record_first
] = s
->buffer_record
;
552 s
->buffer_record
= t
;
554 /* record list is a circular queue */
556 if (s
->record_first
>= s
->record_count
) s
->record_first
= 0;
558 *mid
= s
->buffer_record
->mid
;
564 * Decodes a record structure.
567 asl_mini_memory_message_decode(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, aslmsg
*out
)
572 const char *key
, *val
;
574 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
575 if (r
== NULL
) return ASL_STATUS_INVALID_ARG
;
576 if (out
== NULL
) return ASL_STATUS_INVALID_ARG
;
580 msg
= asl_new(ASL_TYPE_MSG
);
581 if (msg
== NULL
) return ASL_STATUS_NO_MEMORY
;
584 snprintf(tmp
, sizeof(tmp
), "%u", r
->mid
);
585 asl_set(msg
, ASL_KEY_MSG_ID
, tmp
);
588 snprintf(tmp
, sizeof(tmp
), "%u", r
->level
);
589 asl_set(msg
, ASL_KEY_LEVEL
, tmp
);
592 if (r
->time
!= (uint64_t)-1)
594 snprintf(tmp
, sizeof(tmp
), "%llu", r
->time
);
595 asl_set(msg
, ASL_KEY_TIME
, tmp
);
599 if (r
->nano
!= (uint32_t)-1)
601 snprintf(tmp
, sizeof(tmp
), "%u", r
->nano
);
602 asl_set(msg
, ASL_KEY_TIME_NSEC
, tmp
);
606 if (r
->sender
!= NULL
)
608 asl_set(msg
, ASL_KEY_SENDER
, r
->sender
->str
);
612 if (r
->facility
!= NULL
)
614 asl_set(msg
, ASL_KEY_FACILITY
, r
->facility
->str
);
620 snprintf(tmp
, sizeof(tmp
), "%d", r
->pid
);
621 asl_set(msg
, ASL_KEY_PID
, tmp
);
625 if (r
->message
!= NULL
)
627 asl_set(msg
, ASL_KEY_MSG
, r
->message
->str
);
630 /* Key - Value List */
631 for (i
= 0; i
< r
->kvcount
; i
++)
636 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) key
= r
->kvlist
[i
]->str
;
638 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) val
= r
->kvlist
[i
]->str
;
640 if (key
!= NULL
) asl_set(msg
, key
, val
);
644 return ASL_STATUS_OK
;
648 asl_mini_memory_fetch(asl_mini_memory_t
*s
, uint64_t mid
, aslmsg
*msg
)
652 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
653 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
655 for (i
= 0; i
< s
->record_count
; i
++)
657 if (s
->record
[i
]->mid
== 0) break;
658 if (s
->record
[i
]->mid
== mid
) return asl_mini_memory_message_decode(s
, s
->record
[i
], msg
);
661 return ASL_STATUS_INVALID_ID
;
664 static mini_mem_record_t
*
665 asl_mini_memory_query_to_record(asl_mini_memory_t
*s
, asl_msg_t
*q
, uint32_t *type
)
667 mini_mem_record_t
*out
;
669 mini_mem_string_t
*mkey
, *mval
;
670 const char *key
, *val
;
672 if (type
== NULL
) return NULL
;
676 *type
= ASL_QUERY_MATCH_ERROR
;
680 /* NULL query matches anything */
681 *type
= ASL_QUERY_MATCH_TRUE
;
682 if (q
== NULL
) return NULL
;
683 if (asl_msg_count((asl_msg_t
*)q
) == 0) return NULL
;
686 /* we can only do fast match on equality tests */
687 *type
= ASL_QUERY_MATCH_SLOW
;
689 for (x
= asl_msg_fetch((asl_msg_t
*)q
, 0, NULL
, NULL
, &op
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)q
, x
, NULL
, NULL
, &op
))
691 if (op
!= ASL_QUERY_OP_EQUAL
) return NULL
;
694 out
= (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
697 *type
= ASL_QUERY_MATCH_ERROR
;
701 for (x
= asl_msg_fetch((asl_msg_t
*)q
, 0, &key
, &val
, &op
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)q
, x
, &key
, &val
, &op
))
703 if (key
== NULL
) continue;
705 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
707 if (val
== NULL
) continue;
709 if (*type
& ASL_QUERY_MATCH_MSG_ID
)
711 asl_mini_memory_record_free(s
, out
);
712 *type
= ASL_QUERY_MATCH_SLOW
;
716 *type
|= ASL_QUERY_MATCH_MSG_ID
;
717 out
->mid
= atoll(val
);
719 else if (!strcmp(key
, ASL_KEY_TIME
))
721 if (val
== NULL
) continue;
723 if (*type
& ASL_QUERY_MATCH_TIME
)
725 asl_mini_memory_record_free(s
, out
);
726 *type
= ASL_QUERY_MATCH_SLOW
;
730 *type
|= ASL_QUERY_MATCH_TIME
;
731 out
->time
= asl_parse_time(val
);
733 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
735 if (val
== NULL
) continue;
737 if (*type
& ASL_QUERY_MATCH_NANO
)
739 asl_mini_memory_record_free(s
, out
);
740 *type
= ASL_QUERY_MATCH_SLOW
;
744 *type
|= ASL_QUERY_MATCH_NANO
;
745 out
->nano
= atoll(val
);
747 else if (!strcmp(key
, ASL_KEY_LEVEL
))
749 if (val
== NULL
) continue;
751 if (*type
& ASL_QUERY_MATCH_LEVEL
)
753 asl_mini_memory_record_free(s
, out
);
754 *type
= ASL_QUERY_MATCH_SLOW
;
758 *type
|= ASL_QUERY_MATCH_LEVEL
;
759 out
->level
= atoi(val
);
761 else if (!strcmp(key
, ASL_KEY_PID
))
763 if (val
== NULL
) continue;
765 if (*type
& ASL_QUERY_MATCH_PID
)
767 asl_mini_memory_record_free(s
, out
);
768 *type
= ASL_QUERY_MATCH_SLOW
;
772 *type
|= ASL_QUERY_MATCH_PID
;
773 out
->pid
= atoi(val
);
775 else if (!strcmp(key
, ASL_KEY_SENDER
))
777 if (val
== NULL
) continue;
779 if (*type
& ASL_QUERY_MATCH_SENDER
)
781 asl_mini_memory_record_free(s
, out
);
782 *type
= ASL_QUERY_MATCH_SLOW
;
786 *type
|= ASL_QUERY_MATCH_SENDER
;
787 out
->sender
= asl_mini_memory_string_retain(s
, val
, 0);
788 if (out
->sender
== NULL
)
790 asl_mini_memory_record_free(s
, out
);
791 *type
= ASL_QUERY_MATCH_FALSE
;
795 else if (!strcmp(key
, ASL_KEY_FACILITY
))
797 if (val
== NULL
) continue;
799 if (*type
& ASL_QUERY_MATCH_FACILITY
)
801 asl_mini_memory_record_free(s
, out
);
802 *type
= ASL_QUERY_MATCH_SLOW
;
806 *type
|= ASL_QUERY_MATCH_FACILITY
;
807 out
->facility
= asl_mini_memory_string_retain(s
, val
, 0);
808 if (out
->facility
== NULL
)
810 asl_mini_memory_record_free(s
, out
);
811 *type
= ASL_QUERY_MATCH_FALSE
;
815 else if (!strcmp(key
, ASL_KEY_MSG
))
817 if (val
== NULL
) continue;
819 if (*type
& ASL_QUERY_MATCH_MESSAGE
)
821 asl_mini_memory_record_free(s
, out
);
822 *type
= ASL_QUERY_MATCH_SLOW
;
826 *type
|= ASL_QUERY_MATCH_MESSAGE
;
827 out
->message
= asl_mini_memory_string_retain(s
, val
, 0);
828 if (out
->message
== NULL
)
830 asl_mini_memory_record_free(s
, out
);
831 *type
= ASL_QUERY_MATCH_FALSE
;
837 mkey
= asl_mini_memory_string_retain(s
, key
, 0);
840 asl_mini_memory_record_free(s
, out
);
841 *type
= ASL_QUERY_MATCH_FALSE
;
845 for (i
= 0; i
< out
->kvcount
; i
+= 2)
847 if (out
->kvlist
[i
] == mkey
)
849 asl_mini_memory_record_free(s
, out
);
850 *type
= ASL_QUERY_MATCH_SLOW
;
855 mval
= asl_mini_memory_string_retain(s
, val
, 0);
857 if (out
->kvcount
== 0)
859 out
->kvlist
= (mini_mem_string_t
**)calloc(2, sizeof(mini_mem_string_t
*));
863 out
->kvlist
= (mini_mem_string_t
**)realloc(out
->kvlist
, (out
->kvcount
+ 2) * sizeof(mini_mem_string_t
*));
866 if (out
->kvlist
== NULL
)
868 asl_mini_memory_record_free(s
, out
);
869 *type
= ASL_QUERY_MATCH_ERROR
;
873 out
->kvlist
[out
->kvcount
++] = mkey
;
874 out
->kvlist
[out
->kvcount
++] = mval
;
882 asl_mini_memory_fast_match(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, uint32_t qtype
, mini_mem_record_t
*q
)
886 if (s
== NULL
) return 0;
887 if (r
== NULL
) return 0;
888 if (q
== NULL
) return 1;
890 if ((qtype
& ASL_QUERY_MATCH_MSG_ID
) && (q
->mid
!= r
->mid
)) return 0;
891 if ((qtype
& ASL_QUERY_MATCH_TIME
) && (q
->time
!= r
->time
)) return 0;
892 if ((qtype
& ASL_QUERY_MATCH_NANO
) && (q
->nano
!= r
->nano
)) return 0;
893 if ((qtype
& ASL_QUERY_MATCH_LEVEL
) && (q
->level
!= r
->level
)) return 0;
894 if ((qtype
& ASL_QUERY_MATCH_PID
) && (q
->pid
!= r
->pid
)) return 0;
895 if ((qtype
& ASL_QUERY_MATCH_SENDER
) && (q
->sender
!= r
->sender
)) return 0;
896 if ((qtype
& ASL_QUERY_MATCH_FACILITY
) && (q
->facility
!= r
->facility
)) return 0;
897 if ((qtype
& ASL_QUERY_MATCH_MESSAGE
) && (q
->message
!= r
->message
)) return 0;
899 for (i
= 0; i
< q
->kvcount
; i
+= 2)
901 for (j
= 0; j
< r
->kvcount
; j
+= 2)
903 if (q
->kvlist
[i
] == r
->kvlist
[j
])
905 if (q
->kvlist
[i
+ 1] == r
->kvlist
[j
+ 1]) break;
910 if (j
>= r
->kvcount
) return 0;
917 asl_mini_memory_slow_match(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, mini_mem_record_t
*q
, aslmsg rawq
)
923 status
= asl_mini_memory_message_decode(s
, r
, &rawm
);
924 if (status
!= ASL_STATUS_OK
) return 0;
927 if (asl_msg_cmp((asl_msg_t
*)rawq
, (asl_msg_t
*)rawm
) != 0) status
= 1;
933 asl_mini_memory_match(asl_mini_memory_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
)
935 uint32_t status
, i
, where
, start
, j
, do_match
, did_match
, rescount
, *qtype
;
936 mini_mem_record_t
**qp
;
939 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
940 if (res
== NULL
) return ASL_STATUS_INVALID_ARG
;
946 if ((query
== NULL
) || ((query
!= NULL
) && (query
->count
== 0)))
952 qp
= (mini_mem_record_t
**)calloc(query
->count
, sizeof(mini_mem_record_t
*));
953 if (qp
== NULL
) return ASL_STATUS_NO_MEMORY
;
955 qtype
= (uint32_t *)calloc(query
->count
, sizeof(uint32_t));
959 return ASL_STATUS_NO_MEMORY
;
963 for (i
= 0; i
< query
->count
; i
++)
965 qp
[i
] = asl_mini_memory_query_to_record(s
, query
->msg
[i
], &(qtype
[i
]));
966 if (qtype
[i
] == ASL_QUERY_MATCH_ERROR
)
968 for (j
= 0; j
< i
; j
++) asl_mini_memory_record_free(s
, qp
[j
]);
971 return ASL_STATUS_FAILED
;
974 if (qtype
[i
] != ASL_QUERY_MATCH_TRUE
) do_match
= 1;
978 for (i
= 0; i
< s
->record_count
; i
++)
982 where
= (s
->record_first
+ i
) % s
->record_count
;
983 if (s
->record
[where
]->mid
== 0) continue;
984 if (s
->record
[where
]->mid
>= start_id
) break;
988 where
= ((s
->record_count
- (i
+ 1)) + s
->record_first
) % s
->record_count
;
989 if (s
->record
[where
]->mid
== 0) continue;
990 if (s
->record
[where
]->mid
<= start_id
) break;
994 if (i
>= s
->record_count
)
998 for (i
= 0; i
< query
->count
; i
++) asl_mini_memory_record_free(s
, qp
[i
]);
1003 return ASL_STATUS_OK
;
1009 * loop through records
1011 for (i
= 0; i
< s
->record_count
; i
++)
1013 if (s
->record
[where
]->mid
== 0)
1018 if (where
>= s
->record_count
) where
= 0;
1022 if (where
== 0) where
= s
->record_count
- 1;
1026 if (where
== s
->record_first
) break;
1030 s
->record
[where
]->flags
&= ASL_MINI_MSG_FLAG_SEARCH_CLEAR
;
1031 *last_id
= s
->record
[where
]->mid
;
1038 for (j
= 0; (j
< query
->count
) && (did_match
== 0); j
++)
1040 if (qtype
[j
] == ASL_QUERY_MATCH_TRUE
)
1044 else if (qtype
[j
] == ASL_QUERY_MATCH_FALSE
)
1048 else if (qtype
[j
] == ASL_QUERY_MATCH_SLOW
)
1050 did_match
= asl_mini_memory_slow_match(s
, s
->record
[where
], qp
[j
], (aslmsg
)query
->msg
[j
]);
1054 did_match
= asl_mini_memory_fast_match(s
, s
->record
[where
], qtype
[j
], qp
[j
]);
1061 s
->record
[where
]->flags
|= ASL_MINI_MSG_FLAG_SEARCH_MATCH
;
1063 if ((count
!= 0) && (rescount
>= count
)) break;
1069 if (where
>= s
->record_count
) where
= 0;
1073 if (where
== 0) where
= s
->record_count
- 1;
1077 if (where
== s
->record_first
) break;
1082 for (i
= 0; i
< query
->count
; i
++) asl_mini_memory_record_free(s
, qp
[i
]);
1088 if (rescount
== 0) return ASL_STATUS_OK
;
1090 *res
= (asl_msg_list_t
*)calloc(1, sizeof(asl_msg_list_t
));
1091 if (*res
== NULL
) return ASL_STATUS_NO_MEMORY
;
1093 (*res
)->count
= rescount
;
1095 (*res
)->msg
= (asl_msg_t
**)calloc(rescount
, sizeof(asl_msg_t
*));
1096 if ((*res
)->msg
== NULL
)
1100 return ASL_STATUS_NO_MEMORY
;
1106 if (s
->record
[where
]->flags
& ASL_MINI_MSG_FLAG_SEARCH_MATCH
)
1108 s
->record
[where
]->flags
&= ASL_MINI_MSG_FLAG_SEARCH_CLEAR
;
1110 status
= asl_mini_memory_message_decode(s
, s
->record
[where
], &m
);
1111 if (status
!= ASL_STATUS_OK
)
1113 aslresponse_free(*res
);
1118 (*res
)->msg
[(*res
)->curr
++] = (asl_msg_t
*)m
;
1119 if ((*res
)->curr
== rescount
) break;
1125 if (where
>= s
->record_count
) where
= 0;
1129 if (where
== 0) where
= s
->record_count
- 1;
1133 if (where
== s
->record_first
) break;
1137 return ASL_STATUS_OK
;