xref: /libCEED/backends/memcheck/ceed-memcheck-qfunctioncontext.c (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
20f58c348SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30f58c348SJeremy L Thompson //
40f58c348SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50f58c348SJeremy L Thompson //
60f58c348SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70f58c348SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
90f58c348SJeremy L Thompson #include <ceed/backend.h>
1049aac155SJeremy L Thompson #include <stdbool.h>
110f58c348SJeremy L Thompson #include <string.h>
120f58c348SJeremy L Thompson #include <valgrind/memcheck.h>
132b730f8bSJeremy L Thompson 
140f58c348SJeremy L Thompson #include "ceed-memcheck.h"
150f58c348SJeremy L Thompson 
160f58c348SJeremy L Thompson //------------------------------------------------------------------------------
170f58c348SJeremy L Thompson // QFunctionContext has valid data
180f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextHasValidData_Memcheck(CeedQFunctionContext ctx,bool * has_valid_data)192b730f8bSJeremy L Thompson static int CeedQFunctionContextHasValidData_Memcheck(CeedQFunctionContext ctx, bool *has_valid_data) {
200f58c348SJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
21ad70ee2cSJeremy L Thompson 
229a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
230307dd02SJeremy L Thompson   *has_valid_data = !!impl->data_allocated;
240f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
250f58c348SJeremy L Thompson }
260f58c348SJeremy L Thompson 
270f58c348SJeremy L Thompson //------------------------------------------------------------------------------
280f58c348SJeremy L Thompson // QFunctionContext has borrowed data
290f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextHasBorrowedDataOfType_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type,bool * has_borrowed_data_of_type)302b730f8bSJeremy L Thompson static int CeedQFunctionContextHasBorrowedDataOfType_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type, bool *has_borrowed_data_of_type) {
31ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
32ad70ee2cSJeremy L Thompson 
336e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
340307dd02SJeremy L Thompson 
350307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
360307dd02SJeremy L Thompson   *has_borrowed_data_of_type = !!impl->data_borrowed;
370f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
380f58c348SJeremy L Thompson }
390f58c348SJeremy L Thompson 
400f58c348SJeremy L Thompson //------------------------------------------------------------------------------
410f58c348SJeremy L Thompson // QFunctionContext Set Data
420f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSetData_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type,CeedCopyMode copy_mode,void * data)432b730f8bSJeremy L Thompson static int CeedQFunctionContextSetData_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, void *data) {
44ad70ee2cSJeremy L Thompson   size_t                         ctx_size;
45ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
46ad70ee2cSJeremy L Thompson 
470307dd02SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
480307dd02SJeremy L Thompson 
499a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
50ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
510f58c348SJeremy L Thompson 
520307dd02SJeremy L Thompson   // Clear previous owned data buffers
530307dd02SJeremy L Thompson   if (impl->data_allocated) {
540307dd02SJeremy L Thompson     memset(impl->data_allocated, -42, ctx_size);
550307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->allocated_block_id);
560307dd02SJeremy L Thompson   }
572b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_allocated));
580307dd02SJeremy L Thompson   if (impl->data_owned) {
590307dd02SJeremy L Thompson     memset(impl->data_owned, -42, ctx_size);
600307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->owned_block_id);
610307dd02SJeremy L Thompson   }
622b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_owned));
630307dd02SJeremy L Thompson 
640307dd02SJeremy L Thompson   // Clear borrowed block id, if present
650307dd02SJeremy L Thompson   if (impl->data_borrowed) VALGRIND_DISCARD(impl->borrowed_block_id);
660307dd02SJeremy L Thompson 
670307dd02SJeremy L Thompson   // Set internal pointers to external buffers
680f58c348SJeremy L Thompson   switch (copy_mode) {
690f58c348SJeremy L Thompson     case CEED_COPY_VALUES:
700307dd02SJeremy L Thompson       impl->data_owned    = NULL;
710f58c348SJeremy L Thompson       impl->data_borrowed = NULL;
720f58c348SJeremy L Thompson       break;
730f58c348SJeremy L Thompson     case CEED_OWN_POINTER:
740f58c348SJeremy L Thompson       impl->data_owned     = data;
750f58c348SJeremy L Thompson       impl->data_borrowed  = NULL;
760307dd02SJeremy L Thompson       impl->owned_block_id = VALGRIND_CREATE_BLOCK(impl->data_owned, ctx_size, "Owned external data buffer");
770f58c348SJeremy L Thompson       break;
780f58c348SJeremy L Thompson     case CEED_USE_POINTER:
790307dd02SJeremy L Thompson       impl->data_owned     = NULL;
800f58c348SJeremy L Thompson       impl->data_borrowed  = data;
810307dd02SJeremy L Thompson       impl->owned_block_id = VALGRIND_CREATE_BLOCK(impl->data_borrowed, ctx_size, "Borrowed external data buffer");
820f58c348SJeremy L Thompson   }
830307dd02SJeremy L Thompson 
840307dd02SJeremy L Thompson   // Create internal data buffer
852b730f8bSJeremy L Thompson   CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_allocated));
860307dd02SJeremy L Thompson   impl->allocated_block_id = VALGRIND_CREATE_BLOCK(impl->data_allocated, ctx_size, "'Allocated internal context data buffer");
870307dd02SJeremy L Thompson   memcpy(impl->data_allocated, data, ctx_size);
880307dd02SJeremy L Thompson   return CEED_ERROR_SUCCESS;
890307dd02SJeremy L Thompson }
900307dd02SJeremy L Thompson 
910307dd02SJeremy L Thompson //------------------------------------------------------------------------------
920307dd02SJeremy L Thompson // Sync data
930307dd02SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextSyncData_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type)940307dd02SJeremy L Thompson static int CeedQFunctionContextSyncData_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type) {
950307dd02SJeremy L Thompson   size_t                         ctx_size;
960307dd02SJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
970307dd02SJeremy L Thompson 
980307dd02SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
990307dd02SJeremy L Thompson 
1000307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1010307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
1020307dd02SJeremy L Thompson 
1030307dd02SJeremy L Thompson   // Copy internal buffer back to owned or borrowed data buffer
1040307dd02SJeremy L Thompson   if (impl->data_owned) {
1050307dd02SJeremy L Thompson     memcpy(impl->data_owned, impl->data_allocated, ctx_size);
1060307dd02SJeremy L Thompson   }
1070307dd02SJeremy L Thompson   if (impl->data_borrowed) {
1080307dd02SJeremy L Thompson     memcpy(impl->data_borrowed, impl->data_allocated, ctx_size);
1090307dd02SJeremy L Thompson   }
1100f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1110f58c348SJeremy L Thompson }
1120f58c348SJeremy L Thompson 
1130f58c348SJeremy L Thompson //------------------------------------------------------------------------------
1140f58c348SJeremy L Thompson // QFunctionContext Take Data
1150f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextTakeData_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type,void * data)1162b730f8bSJeremy L Thompson static int CeedQFunctionContextTakeData_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
1170307dd02SJeremy L Thompson   size_t                         ctx_size;
118ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
119ad70ee2cSJeremy L Thompson 
1206e536b99SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only provide HOST memory for this backend");
1210f58c348SJeremy L Thompson 
1220307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1230307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
1240307dd02SJeremy L Thompson 
1250307dd02SJeremy L Thompson   // Synchronize memory
1260307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSyncData_Memcheck(ctx, CEED_MEM_HOST));
1270307dd02SJeremy L Thompson 
1280307dd02SJeremy L Thompson   // Return borrowed buffer
1290f58c348SJeremy L Thompson   *(void **)data      = impl->data_borrowed;
1300f58c348SJeremy L Thompson   impl->data_borrowed = NULL;
1310307dd02SJeremy L Thompson   VALGRIND_DISCARD(impl->borrowed_block_id);
1320307dd02SJeremy L Thompson 
1330307dd02SJeremy L Thompson   // De-allocate internal memory
1340307dd02SJeremy L Thompson   if (impl->data_allocated) {
1350307dd02SJeremy L Thompson     memset(impl->data_allocated, -42, ctx_size);
1360307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->allocated_block_id);
1370307dd02SJeremy L Thompson   }
1382b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_allocated));
1390f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1400f58c348SJeremy L Thompson }
1410f58c348SJeremy L Thompson 
1420f58c348SJeremy L Thompson //------------------------------------------------------------------------------
1430f58c348SJeremy L Thompson // QFunctionContext Get Data
1440f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextGetData_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type,void * data)1452b730f8bSJeremy L Thompson static int CeedQFunctionContextGetData_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
1460307dd02SJeremy L Thompson   size_t                         ctx_size;
147ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
148ad70ee2cSJeremy L Thompson 
1490307dd02SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
1500307dd02SJeremy L Thompson 
1519a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1520307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
1530f58c348SJeremy L Thompson 
1540307dd02SJeremy L Thompson   // Create and return writable buffer
1550307dd02SJeremy L Thompson   CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_writable_copy));
1560307dd02SJeremy L Thompson   impl->writable_block_id = VALGRIND_CREATE_BLOCK(impl->data_writable_copy, ctx_size, "Allocated writeable data buffer copy");
1570307dd02SJeremy L Thompson   memcpy(impl->data_writable_copy, impl->data_allocated, ctx_size);
1580307dd02SJeremy L Thompson   *(void **)data = impl->data_writable_copy;
1590f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1600f58c348SJeremy L Thompson }
1610f58c348SJeremy L Thompson 
1620f58c348SJeremy L Thompson //------------------------------------------------------------------------------
1638e457467SJeremy L Thompson // QFunctionContext Get Data Read-Only
1648e457467SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextGetDataRead_Memcheck(CeedQFunctionContext ctx,CeedMemType mem_type,void * data)1652b730f8bSJeremy L Thompson static int CeedQFunctionContextGetDataRead_Memcheck(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) {
166ad70ee2cSJeremy L Thompson   size_t                         ctx_size;
167ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
1688e457467SJeremy L Thompson 
1690307dd02SJeremy L Thompson   CeedCheck(mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Can only set HOST memory for this backend");
1700307dd02SJeremy L Thompson 
1719a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
172ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
1738e457467SJeremy L Thompson 
1740307dd02SJeremy L Thompson   // Create and return read-only buffer
1750307dd02SJeremy L Thompson   if (!impl->data_read_only_copy) {
1762b730f8bSJeremy L Thompson     CeedCallBackend(CeedMallocArray(1, ctx_size, &impl->data_read_only_copy));
1770307dd02SJeremy L Thompson     impl->writable_block_id = VALGRIND_CREATE_BLOCK(impl->data_read_only_copy, ctx_size, "Allocated read-only data buffer copy");
1780307dd02SJeremy L Thompson     memcpy(impl->data_read_only_copy, impl->data_allocated, ctx_size);
1790307dd02SJeremy L Thompson   }
1800307dd02SJeremy L Thompson   *(void **)data = impl->data_read_only_copy;
1818e457467SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1828e457467SJeremy L Thompson }
1838e457467SJeremy L Thompson 
1848e457467SJeremy L Thompson //------------------------------------------------------------------------------
1850f58c348SJeremy L Thompson // QFunctionContext Restore Data
1860f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextRestoreData_Memcheck(CeedQFunctionContext ctx)1870f58c348SJeremy L Thompson static int CeedQFunctionContextRestoreData_Memcheck(CeedQFunctionContext ctx) {
1880f58c348SJeremy L Thompson   size_t                         ctx_size;
1890f58c348SJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
190ad70ee2cSJeremy L Thompson 
191ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
1929a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
1930f58c348SJeremy L Thompson 
1940307dd02SJeremy L Thompson   // Copy back to internal buffer and sync
1950307dd02SJeremy L Thompson   memcpy(impl->data_allocated, impl->data_writable_copy, ctx_size);
1960307dd02SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSyncData_Memcheck(ctx, CEED_MEM_HOST));
1970307dd02SJeremy L Thompson 
1980307dd02SJeremy L Thompson   // Invalidate writable buffer
1990307dd02SJeremy L Thompson   memset(impl->data_writable_copy, -42, ctx_size);
2000307dd02SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_writable_copy));
2010307dd02SJeremy L Thompson   VALGRIND_DISCARD(impl->writable_block_id);
2020f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2030f58c348SJeremy L Thompson }
2040f58c348SJeremy L Thompson 
2050f58c348SJeremy L Thompson //------------------------------------------------------------------------------
2068e457467SJeremy L Thompson // QFunctionContext Restore Data Read-Only
2078e457467SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextRestoreDataRead_Memcheck(CeedQFunctionContext ctx)2082b730f8bSJeremy L Thompson static int CeedQFunctionContextRestoreDataRead_Memcheck(CeedQFunctionContext ctx) {
209ad70ee2cSJeremy L Thompson   size_t                         ctx_size;
210ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
211ad70ee2cSJeremy L Thompson 
212ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size));
2139a25c351SJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2148e457467SJeremy L Thompson 
2150307dd02SJeremy L Thompson   // Verify no changes made during read-only access
2160307dd02SJeremy L Thompson   bool is_changed = memcmp(impl->data_allocated, impl->data_read_only_copy, ctx_size);
2178e457467SJeremy L Thompson 
2189bc66399SJeremy L Thompson   CeedCheck(!is_changed, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Context data changed while accessed in read-only mode");
2190307dd02SJeremy L Thompson 
2200307dd02SJeremy L Thompson   // Invalidate read-only buffer
2210307dd02SJeremy L Thompson   memset(impl->data_read_only_copy, -42, ctx_size);
2222b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->data_read_only_copy));
2230307dd02SJeremy L Thompson   VALGRIND_DISCARD(impl->read_only_block_id);
2248e457467SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2258e457467SJeremy L Thompson }
2268e457467SJeremy L Thompson 
2278e457467SJeremy L Thompson //------------------------------------------------------------------------------
2282e64a2b9SJeremy L Thompson // QFunctionContext destroy user data
2292e64a2b9SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextDataDestroy_Memcheck(CeedQFunctionContext ctx)2302e64a2b9SJeremy L Thompson static int CeedQFunctionContextDataDestroy_Memcheck(CeedQFunctionContext ctx) {
231ad70ee2cSJeremy L Thompson   CeedMemType                         data_destroy_mem_type;
232ad70ee2cSJeremy L Thompson   CeedQFunctionContextDataDestroyUser data_destroy_function;
233ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck      *impl;
234ad70ee2cSJeremy L Thompson 
235ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2360307dd02SJeremy L Thompson 
237ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function));
2389bc66399SJeremy L Thompson   CeedCheck(data_destroy_mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND,
2399bc66399SJeremy L Thompson             "Can only destroy HOST memory for this backend");
2402e64a2b9SJeremy L Thompson 
2410307dd02SJeremy L Thompson   // Run user destroy routine
2422e64a2b9SJeremy L Thompson   if (data_destroy_function) {
2430307dd02SJeremy L Thompson     bool is_borrowed = !!impl->data_borrowed;
2440307dd02SJeremy L Thompson 
2450307dd02SJeremy L Thompson     CeedCallBackend(data_destroy_function(is_borrowed ? impl->data_borrowed : impl->data_owned));
2460307dd02SJeremy L Thompson     if (is_borrowed) VALGRIND_DISCARD(impl->borrowed_block_id);
2470307dd02SJeremy L Thompson     else VALGRIND_DISCARD(impl->owned_block_id);
2482e64a2b9SJeremy L Thompson   }
2490307dd02SJeremy L Thompson   // Free allocations and discard block ids
2500307dd02SJeremy L Thompson   if (impl->data_allocated) {
2512b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->data_allocated));
2520307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->allocated_block_id);
2530307dd02SJeremy L Thompson   }
2540307dd02SJeremy L Thompson   if (impl->data_owned) {
2550307dd02SJeremy L Thompson     CeedCallBackend(CeedFree(&impl->data_owned));
2560307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->owned_block_id);
2570307dd02SJeremy L Thompson   }
2580307dd02SJeremy L Thompson   if (impl->data_borrowed) {
2590307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->borrowed_block_id);
2600307dd02SJeremy L Thompson   }
2612e64a2b9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2622e64a2b9SJeremy L Thompson }
2632e64a2b9SJeremy L Thompson 
2642e64a2b9SJeremy L Thompson //------------------------------------------------------------------------------
2650f58c348SJeremy L Thompson // QFunctionContext Destroy
2660f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextDestroy_Memcheck(CeedQFunctionContext ctx)2670f58c348SJeremy L Thompson static int CeedQFunctionContextDestroy_Memcheck(CeedQFunctionContext ctx) {
2680f58c348SJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
2690f58c348SJeremy L Thompson 
2700307dd02SJeremy L Thompson   // Free allocations and discard block ids
271ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl));
2720307dd02SJeremy L Thompson   if (impl->data_allocated) {
2732b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->data_allocated));
2740307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->allocated_block_id);
2750307dd02SJeremy L Thompson   }
2760307dd02SJeremy L Thompson   if (impl->data_owned) {
2772b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&impl->data_owned));
2780307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->owned_block_id);
2790307dd02SJeremy L Thompson   }
2800307dd02SJeremy L Thompson   if (impl->data_borrowed) {
2810307dd02SJeremy L Thompson     VALGRIND_DISCARD(impl->borrowed_block_id);
2820307dd02SJeremy L Thompson   }
2832b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
2840f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2850f58c348SJeremy L Thompson }
2860f58c348SJeremy L Thompson 
2870f58c348SJeremy L Thompson //------------------------------------------------------------------------------
2880f58c348SJeremy L Thompson // QFunctionContext Create
2890f58c348SJeremy L Thompson //------------------------------------------------------------------------------
CeedQFunctionContextCreate_Memcheck(CeedQFunctionContext ctx)2900f58c348SJeremy L Thompson int CeedQFunctionContextCreate_Memcheck(CeedQFunctionContext ctx) {
2910f58c348SJeremy L Thompson   Ceed                           ceed;
292ad70ee2cSJeremy L Thompson   CeedQFunctionContext_Memcheck *impl;
2930f58c348SJeremy L Thompson 
294ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed));
2952b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasValidData", CeedQFunctionContextHasValidData_Memcheck));
2962b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "HasBorrowedDataOfType", CeedQFunctionContextHasBorrowedDataOfType_Memcheck));
2972b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "SetData", CeedQFunctionContextSetData_Memcheck));
2982b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "TakeData", CeedQFunctionContextTakeData_Memcheck));
2992b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Memcheck));
3002b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Memcheck));
3012b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Memcheck));
3022b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreDataRead_Memcheck));
3032b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "DataDestroy", CeedQFunctionContextDataDestroy_Memcheck));
3042b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Memcheck));
3059bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
3062b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
3072b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl));
3080f58c348SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3090f58c348SJeremy L Thompson }
3102a86cc9dSSebastian Grimberg 
3110f58c348SJeremy L Thompson //------------------------------------------------------------------------------
312