Searching for bytes in a block of memory

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Searching for bytes in a block of memory

Post by fzabkar »

I have an allocation of memory which contains a block of data consisting of 32 repetitions of 0xFF. I would like to search this memory for this block and position the pointer at the following byte. However, I can't come up with a "clean" solution. Is there a built-in function, or a C function, that can do this?
adeyblue
Posts: 301
Joined: Nov 07, 2019 20:08

Re: Searching for bytes in a block of memory

Post by adeyblue »

In a word, no. You could try Instr, but that might have ssues depending on what is in your memory block and the type it is. But you probably typed more in your question than it will take in code. It's can't be more than a six line while loop.

If they can truly be anywhere, don't forget to account for the fact they may be at the end of your memory block and the following byte does not exist.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Searching for bytes in a block of memory

Post by fzabkar »

This is what I ended up with.

Code: Select all

Dim bHINxPtr As UByte Ptr
Dim qwFFblock( 0 To 3 ) As ULongInt = { &HFFFFFFFFFFFFFFFF, &HFFFFFFFFFFFFFFFF, &HFFFFFFFFFFFFFFFF, &HFFFFFFFFFFFFFFFF}
Dim wdBytOfst As UShort
Dim wdUCodeLoc As UShort

wdUCodeLoc = 0

For wdBytOfst = 0 To ( &H200 - 32 )

   If memcmp( bHINxPtr + wdBytOfst, @qwFFblock( 0 ), 32 ) = 0 Then
   
       wdUCodeLoc = wdBytOfst + 32
       Exit For

   End If

Next wdBytOfst
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Searching for bytes in a block of memory

Post by Munair »

If it's about real memory blocks, rather than array elements, consider allocating memory and scan the block for specific data. It's quite easy to mark the start and end of a block of specific bytes:

Code: Select all

dim as uinteger ptr p
dim as uinteger     null, max
dim as integer      i, x, y
dim as uinteger     size
dim as uinteger     sp1, sp2, sp3, sp4

null = 0
max = -1
size = sizeof(uinteger)

' allocate block
p = allocate(512 * size)
if p = 0 then
  print "memory not allocated"
  end -1
end if

' fill block
for i = 0 to 511
  if i > 299 and i < 304 then
    ' 300 to 303 filled with FFFFFFFFFFFFFFFFh
    p[i] = max
  else
    p[i] = null
  end if
next

x = 0
y = 0

' mark filled block
for i = 0 to 511
  ' y to last filled position
  if p[i] = max then y = i
  ' x to position before filled block
  if y = 0 then x = i
next

' result
sp1 = (@p[511] - @p[0] + 1) * size
print "total block size:             ", sp1
sp2 = (@p[x] - @p[0] + 1) * size
print "space before filled block:    ", sp2
sp3 = (@p[y] - @p[x]) * size
print "space of filled block:        ", sp3
sp4 = (@p[511] - @p[y]) * size
print "space after filled block:     ", sp4
print
print "sum of empty & filled spaces: ", sp2 + sp3 + sp4

deallocate(p)
Post Reply