| 1 | #include "jemalloc/internal/jemalloc_preamble.h" |
| 2 | #include "jemalloc/internal/jemalloc_internal_includes.h" |
| 3 | |
| 4 | #include "jemalloc/internal/log.h" |
| 5 | |
| 6 | char log_var_names[JEMALLOC_LOG_VAR_BUFSIZE]; |
| 7 | atomic_b_t log_init_done = ATOMIC_INIT(false); |
| 8 | |
| 9 | /* |
| 10 | * Returns true if we were able to pick out a segment. Fills in r_segment_end |
| 11 | * with a pointer to the first character after the end of the string. |
| 12 | */ |
| 13 | static const char * |
| 14 | (const char* segment_begin) { |
| 15 | const char *end; |
| 16 | for (end = segment_begin; *end != '\0' && *end != '|'; end++) { |
| 17 | } |
| 18 | return end; |
| 19 | } |
| 20 | |
| 21 | static bool |
| 22 | log_var_matches_segment(const char *segment_begin, const char *segment_end, |
| 23 | const char *log_var_begin, const char *log_var_end) { |
| 24 | assert(segment_begin <= segment_end); |
| 25 | assert(log_var_begin < log_var_end); |
| 26 | |
| 27 | ptrdiff_t segment_len = segment_end - segment_begin; |
| 28 | ptrdiff_t log_var_len = log_var_end - log_var_begin; |
| 29 | /* The special '.' segment matches everything. */ |
| 30 | if (segment_len == 1 && *segment_begin == '.') { |
| 31 | return true; |
| 32 | } |
| 33 | if (segment_len == log_var_len) { |
| 34 | return strncmp(segment_begin, log_var_begin, segment_len) == 0; |
| 35 | } else if (segment_len < log_var_len) { |
| 36 | return strncmp(segment_begin, log_var_begin, segment_len) == 0 |
| 37 | && log_var_begin[segment_len] == '.'; |
| 38 | } else { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | unsigned |
| 44 | log_var_update_state(log_var_t *log_var) { |
| 45 | const char *log_var_begin = log_var->name; |
| 46 | const char *log_var_end = log_var->name + strlen(log_var->name); |
| 47 | |
| 48 | /* Pointer to one before the beginning of the current segment. */ |
| 49 | const char *segment_begin = log_var_names; |
| 50 | |
| 51 | /* |
| 52 | * If log_init done is false, we haven't parsed the malloc conf yet. To |
| 53 | * avoid log-spew, we default to not displaying anything. |
| 54 | */ |
| 55 | if (!atomic_load_b(&log_init_done, ATOMIC_ACQUIRE)) { |
| 56 | return LOG_INITIALIZED_NOT_ENABLED; |
| 57 | } |
| 58 | |
| 59 | while (true) { |
| 60 | const char *segment_end = log_var_extract_segment( |
| 61 | segment_begin); |
| 62 | assert(segment_end < log_var_names + JEMALLOC_LOG_VAR_BUFSIZE); |
| 63 | if (log_var_matches_segment(segment_begin, segment_end, |
| 64 | log_var_begin, log_var_end)) { |
| 65 | atomic_store_u(&log_var->state, LOG_ENABLED, |
| 66 | ATOMIC_RELAXED); |
| 67 | return LOG_ENABLED; |
| 68 | } |
| 69 | if (*segment_end == '\0') { |
| 70 | /* Hit the end of the segment string with no match. */ |
| 71 | atomic_store_u(&log_var->state, |
| 72 | LOG_INITIALIZED_NOT_ENABLED, ATOMIC_RELAXED); |
| 73 | return LOG_INITIALIZED_NOT_ENABLED; |
| 74 | } |
| 75 | /* Otherwise, skip the delimiter and continue. */ |
| 76 | segment_begin = segment_end + 1; |
| 77 | } |
| 78 | } |
| 79 | |