Is there a way of creating a RAWSXP vector that is backed by an existing C char* ptr.
Below I show my current working version which needs to reallocate and copy the bytes, and a second imagined version that doesn't exist.
    // My current slow solution that uses lots of memory
    SEXP getData() {
      // has size, and data
      Response resp = expensive_call();
    
      //COPY OVER BYTE BY BYTE
      SEXP respVec = Rf_allocVector(RAWSXP, resp.size);
      Rbyte* ptr = RAW(respVec);
      memcpy(ptr, resp.msg, resp.size);
    
      // free the memory
      free(resp.data);
    
      return respVec;
    }
    
    // My imagined solution
    SEXP getDataFast() {
      // has size, and data
      Response resp = expensive_call();
    
      // reuse the ptr
      SEXP respVec = Rf_allocVectorViaPtr(RAWSXP, resp.data, resp.size);
    
      return respVec;
    }
I also noticed Rf_allocVector3 which seems to give control over memory allocations of the vector, but I couldn't get this to work.  This is my first time writing an R extension, so I imagine I must be doing something stupid.  I'm trying to avoid the copy as the data will be around a GB (very large, sparse though, matrices).