42 43 #ifdef HB_USE_ATEXIT 44 static 45 void free_static_shapers (void) 46 { 47 if (unlikely (static_shapers != all_shapers)) 48 free ((void *) static_shapers); 49 } 50 #endif 51 52 const hb_shaper_pair_t * 53 _hb_shapers_get (void) 54 { 55 retry: 56 hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); 57 58 if (unlikely (!shapers)) 59 { 60 char *env = getenv ("HB_SHAPER_LIST"); 61 if (!env || !*env) { 62 (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); 63 return (const hb_shaper_pair_t *) all_shapers; 64 } 65 66 /* Not found; allocate one. */ 67 shapers = (hb_shaper_pair_t *) calloc (1, sizeof (all_shapers)); 68 if (unlikely (!shapers)) { 69 (void) hb_atomic_ptr_cmpexch (&static_shapers, NULL, &all_shapers[0]); 70 return (const hb_shaper_pair_t *) all_shapers; 71 } 72 73 memcpy (shapers, all_shapers, sizeof (all_shapers)); 74 75 /* Reorder shaper list to prefer requested shapers. */ 76 unsigned int i = 0; 77 char *end, *p = env; 78 for (;;) { 79 end = strchr (p, ','); 80 if (!end) 81 end = p + strlen (p); 82 83 for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) 84 if (end - p == (int) strlen (shapers[j].name) && 85 0 == strncmp (shapers[j].name, p, end - p)) 86 { 87 /* Reorder this shaper to position i */ 88 struct hb_shaper_pair_t t = shapers[j]; 89 memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); 90 shapers[i] = t; 91 i++; 92 } 93 94 if (!*end) 95 break; 96 else 97 p = end + 1; 98 } 99 100 if (!hb_atomic_ptr_cmpexch (&static_shapers, NULL, shapers)) { 101 free (shapers); 102 goto retry; 103 } 104 105 #ifdef HB_USE_ATEXIT 106 atexit (free_static_shapers); /* First person registers atexit() callback. */ 107 #endif 108 } 109 110 return shapers; 111 } | 42 43 #ifdef HB_USE_ATEXIT 44 static 45 void free_static_shapers (void) 46 { 47 if (unlikely (static_shapers != all_shapers)) 48 free ((void *) static_shapers); 49 } 50 #endif 51 52 const hb_shaper_pair_t * 53 _hb_shapers_get (void) 54 { 55 retry: 56 hb_shaper_pair_t *shapers = (hb_shaper_pair_t *) hb_atomic_ptr_get (&static_shapers); 57 58 if (unlikely (!shapers)) 59 { 60 char *env = getenv ("HB_SHAPER_LIST"); 61 if (!env || !*env) { 62 (void) hb_atomic_ptr_cmpexch (&static_shapers, nullptr, &all_shapers[0]); 63 return (const hb_shaper_pair_t *) all_shapers; 64 } 65 66 /* Not found; allocate one. */ 67 shapers = (hb_shaper_pair_t *) calloc (1, sizeof (all_shapers)); 68 if (unlikely (!shapers)) { 69 (void) hb_atomic_ptr_cmpexch (&static_shapers, nullptr, &all_shapers[0]); 70 return (const hb_shaper_pair_t *) all_shapers; 71 } 72 73 memcpy (shapers, all_shapers, sizeof (all_shapers)); 74 75 /* Reorder shaper list to prefer requested shapers. */ 76 unsigned int i = 0; 77 char *end, *p = env; 78 for (;;) { 79 end = strchr (p, ','); 80 if (!end) 81 end = p + strlen (p); 82 83 for (unsigned int j = i; j < ARRAY_LENGTH (all_shapers); j++) 84 if (end - p == (int) strlen (shapers[j].name) && 85 0 == strncmp (shapers[j].name, p, end - p)) 86 { 87 /* Reorder this shaper to position i */ 88 struct hb_shaper_pair_t t = shapers[j]; 89 memmove (&shapers[i + 1], &shapers[i], sizeof (shapers[i]) * (j - i)); 90 shapers[i] = t; 91 i++; 92 } 93 94 if (!*end) 95 break; 96 else 97 p = end + 1; 98 } 99 100 if (!hb_atomic_ptr_cmpexch (&static_shapers, nullptr, shapers)) { 101 free (shapers); 102 goto retry; 103 } 104 105 #ifdef HB_USE_ATEXIT 106 atexit (free_static_shapers); /* First person registers atexit() callback. */ 107 #endif 108 } 109 110 return shapers; 111 } |