ASPrintf or VSNPrintf

3 posts / 0 new
Last post
jabirulo
jabirulo's picture
Offline
Last seen: 3 days 10 hours ago
Joined: 2013-05-30 00:53
ASPrintf or VSNPrintf

Hi use this piece of code:

  1. ...
  2. if(dd->infowin_autosave) {
  3. char textbuf[16];
  4.  
  5. SNPrintf(textbuf, sizeof(textbuf), "%ld",dd->winX);
  6. saveDockyToolType(dd->GFXDock_ExePath, "WINX", textbuf);
  7. SNPrintf(textbuf, sizeof(textbuf), "%ld",dd->winY);
  8. saveDockyToolType(dd->GFXDock_ExePath, "WINY", textbuf);
  9. }
  10. ...

would be "better" to use ASPrintf instead?

  1. ...
  2. if(dd->infowin_autosave) {
  3. STRPTR textbuf;
  4.  
  5. textbuf = ASPrintf("%ld",dd->winX);
  6. saveDockyToolType(dd->GFXDock_ExePath, "WINX", textbuf);
  7. textbuf = ASPrintf("%ld",dd->winY);
  8. saveDockyToolType(dd->GFXDock_ExePath, "WINY", textbuf);
  9. FreeVec(textbuf);
  10. }
  11. ...
thomas
thomas's picture
Offline
Last seen: 1 week 6 days ago
Joined: 2011-05-16 14:23
Re: ASPrintf or VSNPrintf

Your usage of ASPrintf is bad because it leaks memory. Each call to ASPrintf allocates a new buffer and you have to free them all, not only the last one.

That's also the reason why I would recommend SNPrintf in your case. Allocating and freeing a new buffer each time is quite a bit of overhead which can be saved if you use SNPrintf and reuse one buffer.

The main question is how saveDockyToolType deals with the text buffer. I suppose it copies its contents so that you can indeed reuse the buffer after each call.

jabirulo
jabirulo's picture
Offline
Last seen: 3 days 10 hours ago
Joined: 2013-05-30 00:53
Re: ASPrintf or VSNPrintf

Ooops,. you're tight I missed one FreeVec(().

And thx that what I wanted to know if it's too overhead for so tiny strings/piece of code.

Yes IIRC saveDockyTooltType() makes acopy of data I'm passing.
Not sure, but maybe such function/code was done by you or anyone on this forum ;-):

  1. void saveDockyToolType(STRPTR iconname, STRPTR ttpName, STRPTR ttpArg)
  2. {
  3. STRPTR *oldttp = NULL;
  4. int i;
  5. char newttp[1024], ttpBuf1[64]="", ttpBuf2[64]="";
  6. struct DiskObject *micon = NULL;
  7. //DBUG("START SaveToolType()\n");
  8. if(ttpArg)
  9. {
  10. SNPrintf(newttp, sizeof(newttp), "%s=",ttpName); // "<tooltype>="
  11. SNPrintf(ttpBuf2, sizeof(ttpBuf2), "(%s=",ttpName); // BUF2 = "(<tooltype>="
  12.  
  13. Strlcpy( ttpBuf1, newttp, sizeof(ttpBuf1) ); // BUF1 = NEWTTP
  14. }
  15.  
  16. //Strlcpy( ttpBuf1, newttp, sizeof(ttpBuf1) ); // BUF1 = NEWTTP
  17.  
  18. micon = GetDiskObject(iconname);
  19. if(micon)
  20. {
  21. for(i = 0; micon->do_ToolTypes<em> != NULL; i++) // parse tooltypes
  22. {
  23. //DBUG("%2ld'%s'\n",i,micon->do_ToolTypes<em>);
  24. if( !Strnicmp(micon->do_ToolTypes<em>, ttpBuf1, Strlen(ttpBuf1))
  25. || !Strnicmp(micon->do_ToolTypes<em>, ttpBuf2, Strlen(ttpBuf2)) )
  26. {// Found tooltype
  27. Strlcat( newttp, ttpArg, sizeof(newttp) );
  28. //DBUG("'%s' == '%s'?\n",micon->do_ToolTypes<em>,newttp);
  29. // Normal case tooltype -> tooltype=value OR (tooltype=value)
  30. if( ttpArg && Stricmp(micon->do_ToolTypes<em>, newttp) )
  31. {// Tooltype loaded diffs. from icon's tooltype -> modify tooltype and END
  32. oldttp = micon->do_ToolTypes;
  33. micon->do_ToolTypes<em> = newttp;
  34. PutDiskObject(iconname, micon);
  35. micon->do_ToolTypes = oldttp;
  36. //DBUG("Tooltype modified: '%s'\n",newttp);
  37. }
  38. break;
  39.  
  40. } // END if( !Strnicmp(micon->..
  41. } // END for
  42.  
  43. if(!micon->do_ToolTypes<em>)
  44. {// No tooltype -> create tooltype entry and END
  45. //DBUG("Tooltype '%s' NOT found (i=%ld)\n",newttp,i);
  46. Strlcat( newttp, ttpArg, sizeof(newttp) );
  47. oldttp = micon->do_ToolTypes;
  48. STRPTR *newtts = AllocVecTags( (i+2)*sizeof(STRPTR), TAG_DONE );
  49. CopyMem(oldttp, newtts, i*sizeof(STRPTR) ); // clone tooltypes
  50. newtts<em> = newttp;
  51. newtts[i+1] = NULL;
  52. micon->do_ToolTypes = newtts;
  53. PutDiskObject(iconname, micon);
  54. micon->do_ToolTypes = oldttp;
  55. FreeVec(newtts);
  56. //DBUG("Tooltype created: '%s'\n",newttp);
  57. }
  58.  
  59. FreeDiskObject(micon);
  60. }
  61.  
  62. //DBUG("END SaveToolType()\n");
  63. }

THX

AOS4.1/SAM460ex/PPC460EX-1155MHZ/2048MB/RadeonHD6570/SSD120GB/DVDRW :-P
Log in or register to post comments