/** * @brief Serialize an unsigned 16-bit integer into a managed string. * @note This function will reallocate the output managed string if necessary, and append the serialized data, updating the length field to reflect the changes. * @param data a pointer to the address of a managed string to receive the output, which will be allocated for the caller if NULL is passed. * @param number the value of the unsigned 16-bit integer to be serialized. * @return true if the specified value was serialized and stored successfully, or false on failure. */ bool_t serialize_uint16(stringer_t **data, uint16_t number) { stringer_t *holder = NULL; size_t length = uint16_digits(number); // Make sure we have room to write the number. if (!data) { return false; } else if (!*data && (*data = st_alloc(1024)) == NULL) { return false; } else if (st_avail_get(*data) - st_length_get(*data) < (length+1) && (holder = st_realloc(*data, st_avail_get(*data) + 1024)) == NULL) { return false; } else if (holder) { *data = holder; } if (length != snprintf(st_char_get(*data) + st_length_get(*data), st_avail_get(*data) - st_length_get(*data), "%hu", number)) { return false; } *(st_char_get(*data) + st_length_get(*data) + length) = ';'; st_length_set(*data, st_length_get(*data) + length + 1); return true; }
char *cactusDisk_getStringFromCache(CactusDisk *cactusDisk, Name name, int64_t start, int64_t length, int64_t strand) { /* * Gets a sequence from the cache. */ char *string = NULL; if (stCache_containsRecord(cactusDisk->stringCache, name, start, sizeof(char) * length)) { int64_t recordSize; string = stCache_getRecord(cactusDisk->stringCache, name, start, sizeof(char) * length, &recordSize); assert(string != NULL); assert(recordSize == length); string = st_realloc(string, sizeof(char) * (length + 1)); string[length] = '\0'; if (!strand) { char *string2 = stString_reverseComplementString(string); free(string); string = string2; } } return string; }
/** * @brief Serialize the contents of a managed string into another managed string. * @note This function will reallocate the output managed string if necessary, and append the serialized data, updating the length field to reflect the changes. * @param data a pointer to the address of a managed string to receive the output, which will be allocated for the caller if NULL is passed. * @param string a pointer to a managed string containing the data to be serialized. * @return true if the specified value was serialized and stored successfully, or false on failure. */ bool_t serialize_st(stringer_t **data, stringer_t *string) { size_t length; stringer_t *holder = NULL; // Make sure we have room to write the number. if (!data) { return false; } // Check for NULLs. if (!string || !(length = st_length_get(string))) { if (!serialize_sz(data, 0)) { return false; } return true; } // Record the length. else if (!serialize_sz(data, length)) { return false; } // Previous line ensures that *data should never be NULL. /*else if (!*data && (*data = st_alloc(length + (length % 1024))) == NULL) { return false; }*/ else if (st_avail_get(*data) - st_length_get(*data) < length && (holder = st_realloc(*data, st_avail_get(*data) + length + (length % 1024))) == NULL) { return false; } else if (holder) { *data = holder; } mm_copy(st_char_get(*data) + st_length_get(*data), st_char_get(string), length); *(st_char_get(*data) + st_length_get(*data) + length) = ';'; st_length_set(*data, st_length_get(*data) + length + 1); return true; }
bool_t check_string_realloc(uint32_t check) { size_t len; stringer_t *s, *swap; if (!(s = st_alloc_opts(check, 1)) || !(swap = st_realloc(s, st_length_get(string_check_constant)))) { if (s) { st_free(s); } return false; } // Since mapped allocations are page aligned, we reallocate to a larger than needed size to ensure an actual reallocation occurs. else if ((check & MAPPED_T) && !(swap = st_realloc(s, st_length_get(string_check_constant) + (getpagesize() * 128)))) { if (s) { st_free(s); } return false; } // Jointed strings allow reallocation without changing the address of s, of if the string is jointed and s changes, error out. else if ((check & JOINTED) && swap != s) { st_free(swap); st_free(s); return false; } // Contiguous strings will require the address of s to change, so if it doesn't error out. Except for the mapped type, since the jointed flag doesn't apply to it. else if (!(check & JOINTED) && !(check & MAPPED_T) && swap == s) { st_free(s); return false; } // For contiguous types, free the original string and replace it with the value of swap. else if (swap != s) { st_free(s); s = swap; } len = snprintf(st_char_get(s), st_length_int(string_check_constant) + 1, "%.*s", st_length_int(string_check_constant), st_char_get(string_check_constant)); if ((check & MAPPED_T) || (check & MANAGED_T)) { st_length_set(s, len); } else if (memcmp(st_char_get(s), st_char_get(string_check_constant), st_length_get(string_check_constant))) { return false; } // Enlarge a buffer by a factor of 128 and make sure the data it contained wasn't changed. For managed or mapped strings, multiply the available space. if ((check & (MANAGED_T | MAPPED_T)) && !(swap = st_realloc(s, st_avail_get(s) * 128))) { st_free(s); return false; } // For other string types we multiply the space used by the string since the original allocation size isn't tracked. else if (!(check & (MANAGED_T | MAPPED_T)) && !(swap = st_realloc(s, st_length_get(s) * 128))) { st_free(s); return false; } // Since mapped allocations are page aligned, we reallocate to a larger than needed size to ensure an actual reallocation occurs. else if ((check & MAPPED_T) && !(swap = st_realloc(s, st_length_get(string_check_constant) + (getpagesize() * 128)))) { if (s) { st_free(s); } return false; } // Jointed strings allow reallocation without changing the address of s, of if the string is jointed and s changes, error out. else if ((check & JOINTED) && swap != s) { st_free(swap); st_free(s); return false; } // Contiguous strings will require the address of s to change, so if it doesn't error out. Except for the mapped type, since the jointed flag doesn't apply to it. else if (((check & JOINTED) ^ JOINTED) && !(check & MAPPED_T) && swap == s) { st_free(s); return false; } // For contiguous types, free the original string and replace it with the value of swap. else if (swap != s) { st_free(s); s = swap; } else if (memcmp(st_char_get(s), st_char_get(string_check_constant), st_length_get(string_check_constant))) { return false; } // Now we shrink the buffer back to the bare minimum and check the data one final time. if (!(swap = st_realloc(s, st_length_get(string_check_constant)))) { if (s) { st_free(s); } return false; } else if (swap != s) { st_free(s); s = swap; } st_free(s); return true; }