and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+
+### Added
+- Cgraph's agxbuf API gained a new function agxbdisown(), for dissociating
+ backing memory from the managed buffer
+
+### Changed
- Cgraph's agheap() API has been removed
### Fixed
return -1;
}
+
+char *agxbdisown(agxbuf * xb) {
+
+ size_t size;
+ char *buf;
+
+ /* terminate the existing string */
+ agxbputc(xb, '\0');
+
+ size = (size_t)(xb->ptr - xb->buf);
+
+ if (!xb->dyna) {
+ /* the buffer is not dynamically allocated, so we need to copy its contents
+ * to heap memory
+ */
+
+ buf = malloc(size);
+ if (buf == NULL) {
+ return NULL;
+ }
+
+ memcpy(buf, xb->buf, size);
+
+ } else {
+ /* the buffer is already dynamically allocated, so take it as-is */
+ buf = (char*)xb->buf;
+ }
+
+ /* reset xb to a state where it is usable */
+ xb->buf = xb->ptr = xb->eptr = NULL;
+ xb->dyna = 1;
+
+ return buf;
+}
#define agxbputc(X,C) ((((X)->ptr >= (X)->eptr) ? agxbmore(X,1) : 0), (void)(*(X)->ptr++ = ((unsigned char)C)))
/* agxbuse:
- * Null-terminates buffer; resets and returns pointer to data;
+ * Null-terminates buffer; resets and returns pointer to data. The buffer is
+ * still associated with the agxbuf and will be overwritten on the next, e.g.,
+ * agxbput. If you want to retrieve and disassociate the buffer, use agxbdisown
+ * instead.
* char* agxbuse(agxbuf* xb)
*/
#define agxbuse(X) ((void)agxbputc(X,'\0'),(char*)((X)->ptr = (X)->buf))
*/
#define agxbnext(X) ((char*)((X)->ptr))
+/* agxbdisown:
+ * Disassociate the backing buffer from this agxbuf and return it. The buffer is
+ * NUL terminated before being returned. If the agxbuf is using stack memory,
+ * this will first copy the data to a new heap buffer to then return. If failure
+ * occurs, NULL is returned. If you want to temporarily access the string in the
+ * buffer, but have it overwritten and reused the next time, e.g., agxbput is
+ * called, use agxbuse instead of agxbdisown.
+ */
+ AGXBUF_API char *agxbdisown(agxbuf * xb);
+
#endif
#ifdef __cplusplus